Abide Validation

Abide is an HTML5 form validation library that supports the native API by using patterns and required attributes.

Click Submit to see validation in action

Fieldset
Passwords must be at least 8 characters with 1 capital letter, 1 number, and one special character.
Passwords must match.
Valid email required.
Valid URL required.
Broke.


Setting Up Validation

To enable validation with Abide, add the data-abide attribute to your form element. Then add the required attribute to each input that you want to require. Additionaly, you can define a pattern to define restraints on what users can input.

<form data-abide>
  <div class="name-field">
    <label>Your name <small>required</small></label>
    <input type="text" required pattern="[a-zA-Z]+">
    <small class="error">Name is required and must be a string.</small>
  </div>
  <div class="email-field">
    <label>Email <small>required</small></label>
    <input type="email" required>
    <small class="error">An email address is required.</small>
  </div>
  <button type="submit">Submit</button>
</form>

V3 validation

We are making a new style of validation. Press submit below to see it in action.

To use this style of validation you should add the v3-validation to your form's class attribute.

Fieldset
Passwords must be at least 8 characters with 1 capital letter, 1 number, and one special character.
Passwords must match.
Valid email required.
Valid URL required.
Broke.


Predefined Patterns

Abide has several common validation patterns built in to the library:

Name Valid Example or Format
alpha Foundation
alpha_numeric A1 Sauce
integer -1
number 2937
password LittleW0men.
card visa, amex, mastercard
cvv 384 or 3284
email foundation@zurb.com
url http://www.sonos.com
domain zurb.com
datetime YYYY-MM-DDThh:mm:ssTZD
date YYYY-MM-DD
time HH:MM:SS
dateISO not sure yet
month_day_year MM/DD/YYYY
color #FFF or #FFFFFF

You can also use these patterns by setting the input's type to the name of the pattern instead of using the pattern attribute:

<input type="password" required>
<input type="email">
<input type="url">

There is a good list of valid HTML5 input types on the Mozilla Developer Network. Try avoiding patterns defined by type that do not match the specification.

You can use the following shortcut for named patterns that aren't valid input types:

<input type="text" pattern="integer">

Custom Named Patterns

By overriding Abide during initilization, you can define your own custom patterns or override the default patterns to validate against.

$(document)
  .foundation()
  .foundation('abide', {
    patterns: {
      dashes_only: /^[0-9-]*$/,
      ip_address: /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/
    }
});

You can then use these custom patterns like you would the predfined patterns in your markup:

<form class="custom" data-abide>
  <label>Serial Number</label>
  <input type="text" pattern="dashes_only" required>
</form>

Equal To

To add a confirmation field, you can define the data-equalto attribute.

<form data-abide>
  <div class="password-field">
    <label>Password <small>required</small></label>
    <input type="password" id="password" required pattern="password">
    <small class="error">Your password must match the requirements</small>
  </div>
  <div class="password-confirmation-field">
    <label>Confirm Password <small>required</small></label>
    <input type="password" required pattern="password" data-equalto="password">
    <small class="error">The password did not match</small>
  </div>
  <button type="submit">Submit</button>

Error Messages

To display an error message for your invalid form element, include a small tag with an error class as a sibling of your input.

<div class="input-wrapper">
  <label>Email Address <small>required</small></label>
  <input type="email" required>
  <small class="error">A valid email address is required.</small>
</div>

We have wrapped our input in a div in the example above. This div will receive an error class when the input is invalid. This class will show our error message and style the label and input accordingly.

Invalid inputs inherit a data-invalid attribute.

Events

If a submit event is fired, a valid event is triggered when the form is valid and an invalid event is triggered when the form is invalid.

You can bind to these events and fire your own callback:

$('#myForm')
  .on('invalid.fndtn.abide', function () {
    var invalid_fields = $(this).find('[data-invalid]');
    console.log(invalid_fields);
  });
  .on('valid.fndtn.abide', function () {
    console.log('valid!');
  });

To handle the submit event yourself, use data-abide="ajax" instead of data-abide inside the form tag.

<form data-abide="ajax">
  <div class="name-field">
    <label>Your name <small>required</small></label>
    <input type="text" name="users_name" required pattern="[a-zA-Z]+">
    <small class="error">Name is required and must be a string.</small>
  </div>
  <button type="submit">Submit</button>
</form>