Supply any <form>
with the class "autovalidate"
, include the JS library file jquery.autovalidate.js, place certain CSS classes and custom attributes on your form elements, and (with JS enabled) your rules will be enforced with nice error messages.
<form ... class="autovalidate">
<label>
element, it will have the class "failedvalidation" applied to it; you may use CSS to highlight this field. As of version 1.8.2, this class will also be applied to the form element itself.nicename="..."
name="..."
attribute will be used instead.class="required"
<select>
s, since putting <option value="">(please choose an option)</option>
will not allow the form to be submitted if that option is chosen.requiredmessage
is supplied it will be used for the alert (see below).requiredmessage="..."
<form>
tag itself as a generic fallback message. Used here, instances of %nicename%
appearing in the message will be replaced with the nicename of the element. Here's an example:
<form requiredmessage="Please provide us with your %nicename%; see our Privacy Policy if this concerns you." autovalidate="true">
<input type="text" name="fName" class="required">
<input type="text" name="mName" nicename="Middle Name" class="required">
<input type="text" name="lName" class="required" requiredmessage="Honestly, we *NEED* to know your last name!">
</form>
requiredmessage
attribute was not present on the <form>
above, instead users would see:mustmatchmessage="..."
validateas="..."
or mustmatch="..."
fails to match the criteria, this attribute will be used for the error message. If omitted, a more generic (but still pleasant) error message will be used.validateas="phone"
mustmatchmessage
is not supplied the generic error is "_____ doesn't look like a valid phone number."validateas="email"
mustmatchmessage
is not supplied the generic error is "_____ doesn't look like a valid email address. It must be of the format 'john@host.com'"validateas="date"
mustmatchmessage
is not supplied the generic error is "_____ must be a valid date (e.g. 12/31/2001)"validateas="zipcode"
mustmatchmessage
is not supplied the generic error is "_____ doesn't look like a valid zip code. It should be 5 digits, optionally followed by a dash and four more, e.g. 19009 or 19009-2314"validateas="integer"
mustmatchmessage
is not supplied the generic error is "_____ must be an integer."validateas="float"
mustmatchmessage
is not supplied the generic error is "_____ must be a number, such as 1024 or 3.1415 (no commas are allowed)."mustmatch="..."
mustmatch="please"
will only allow the value "please" to be entered, whereas mustmatch="^[a-zA-Z][a-zA-Z0-9_]*$"
will require the value to begin with a letter and be followed by zero or more numbers, letters, or underscores.mustmatchmessage
is not supplied the generic error is "_____ is not in a valid format."mustmatchcasesensitive="true"
to force a case-sensitive RegExp. Also, the supplied regexp is prefixed with "^" and suffixed with "$" automatically.mustnotmatch="..."
mustmatch
, except the validation fails if the regexp matches the value.mustmatchcasesensitive="true"
mustmatch="..."
is case insensitive by default. Use this attribute to force a case-sensitive match.minvalue="..."
validateas="date"
is supplied, e.g.<input type="text" name="pubdate" nicename="Date Published" validateas="date" minvalue="1/1/1970">
maxvalue="..."
validateas="date"
is supplied, e.g.<input type="text" name="pubdate" nicename="Date Published" validateas="date" maxvalue="10/26/2008">
minlength="..."
maxlength="..."
<input type="checkbox" ... minchosen="...">
nicename
of this checkbox. This attribute (and the nicename
to go with the message) need to be put on the same checkbox, but only need to be put on one of the checkboxes in the set.<input type="checkbox" ... maxchosen="...">
nicename
of this checkbox. This attribute (and the nicename
to go with the message) need to be put on the same checkbox, but only need to be put on one of the checkboxes in the set.<select multiple ... minchosen="...">
nicename
of this select.<select ... maxchosen="...">
nicename
of this select.