Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
296 lines (227 sloc) 13.5 KB
<!DOCTYPE html>
<title>h5Validate - The HTML5 Form Validation Plugin for jQuery</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
<link href="style01.css" media="screen" rel="stylesheet" type="text/css" />
<link href="" rel="stylesheet" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20667798-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
<h1>h5Validate - HTML5 Form Validation for jQuery</h1>
<a class="button" href="">Download from Github</a>
<br /><br />
<p>If you want to contribute, feel free to <a href="">fork the project on Github</a>.</p>
<p>Best practice realtime HTML5 form validation for jQuery. Works on all popular browsers, including old ones like IE6.</p>
<li><p>Regularly tested on 13 different browsers, IE6 - IE9, FireFox, Chrome, iPhone, and Android.</p></li>
<li><p>Implements best practices based on 1,000 user survey, several usability studies, and the behavior of millions of users in live production environments.</p></li>
<div class="announce">
<h3>Supported Platforms</h3>
<p><strong>Desktop:</strong> IE 9, 8, 7, 6, Chrome, Firefox, Safari, and Opera. Tested on Windows 7 and Mac.</p>
<p><strong>Mobile:</strong> iPhone, Android, Palm WebOS</p>
<h3>New in v0.8.0+</h3>
<li>Event API</li>
<h2>Jump Start</h2>
<p>Copy and paste this at the end of the body on any page with an HTML5 form. If html5 validation rules exist in the form, you're in business!</p>
<script src=""></script>
<h3>HTML5 required attribute. e.g.</h3>
<form id="form1" class="h5-defaults">
<label for="name">Your Name:*</label>
<input id="name" name="name" type="text" placeholder="Bob" title="Your name is required." required />
<p><em>Hint: Select it, then click somewhere else without typing anything.</em></p>
<input type="submit" value="Submit" />
<script src=""></script>
<h3>HTMTL5 pattern attribute. e.g.</h3>
<form id="form2" class="h5-defaults">
<label for="birthdate">Your Birth Date:</label>
<input id="birthdate" name="name" type="text" placeholder="mm/dd/yyyy" title="mm/dd/yyyy" pattern="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" />
<p><em>Hint: It's expecting mm/dd/yyyy. Try typing "jQuery rocks!" instead.</em></p>
<script src=""></script>
<h3>Pattern Library</h3>
<p>Sometimes, you'll want to re-use the same validation pattern for several elements on a page. You could copy and paste the same pattern over and over again to a bunch of different form fields, but what happens if you discover a bug in the pattern? Fix it and repeat the whole copy and paste process?</p>
<p>A better solution is to attach the pattern with a class selector. Use <a href="#addPatterns"><code>$.h5Validate.addPatterns()</code></a> to add your pattern, make sure the classPrefix variable is set correctly (it's <code>h5-</code> by default), and add the class to the input fields.</p>
<form class="h5-defaults">
<label for="email">Email:</label>
<input id="email" type="text" class="h5-email" required />
<script src=""></script>
<h3>Error Messages</h3>
<p>Error divs should be hidden in the HTML with <code>display:none;</code> -- h5Validate shows them when the corresponding field is marked invalid.</p>
<form id="form4" class="h5-defaults">
<label for="FirstName">Your Name:*</label>
<input id="FirstName" type="text" data-h5-errorid="invalid-FirstName" title="Required: Please provide your first name." required />
<div id="invalid-FirstName" class="ui-state-error message" style="display:none;">This stuff will get replaced by the title contents.</div>
<p>There are a <em>ton</em> of options you can override by passing key: value pairs when you call .h5Validate(). For example, we can change the CSS class that indicates validation errors:</p>
<form id="black">
<label for="name2">Your Name:*</label>
<input id="name2" name="name2" type="text" placeholder="Bob" title="Your name is required." required />
<p><em>Hint: Select it, then click somewhere else without typing anything.</em></p>
<script src=""></script>
<dt>errorClass</dt><dd><p>Custom CSS class for validation errors.</p></dd>
<dt>validClass</dt><dd><p>Custom CSS class to mark a field validated.</p></dd>
<dt>errorAttribute</dt><dd><p>An html attribute that stores the ID of the error message container for this element. It's set to data-h5-errorid by default. <em>Note: The data- attribute prefix is a new feature of HTML5, used to store an element's meta-data.</em> e.g.</p>
<code class="block">&lt;input id="name" data-h5-errorid="nameError" required &gt;</code>
<dt>kbSelectors</dt><dd><p>A list of CSS selectors for attaching keyboard-oriented events. Default:</p>
<code class="block">kbSelectors: ':text, :password, select, textarea'</code>
<dt>focusout, focusin, change, keyup</dt><dd><p>(Events) These are the "keyboard oriented" events. Better to think of them as non-mouse-specific events. Set them to "true" if you want them to trigger a field validation. Defaults:</p>
<code class="block">focusout: true,<br />
focusin: false,<br />
change: false,<br />
keyup: true</code>
<dt>mSelectors</dt><dd><p>A list of CSS selectors for attaching "mouse oriented" events. Default:</p>
<code class="block">mSelectors: ':radio, :checkbox, select, option'</code>
<dt>click</dt><dd><p>(Event) The only default mouse-oriented event. Since it probably makes little sense to trigger validation on other mouse events, I'll leave it to you to figure out how to enable them.</p>
<p><em>Note: The click event isn't just for the mouse. It will trigger for keyboard and touch screen interactions, too.</em></p>
<code class="block">click: true</code>
<h2>Event API</h2>
<p>h5Validate supports the following events:</p>
<dd>Instance created.</dd>
<dd>The element in question has been validated. A validity object is passed into the event handler containing:
element: HTMLObject, // A reference to the validated element
customError: Bool, // Custom validity failed.
patternMismatch: Bool, // Input does not match pattern
rangeOverflow: Bool, // Input is greater than max attribute value
rangeUnderflow: Bool, // Input is less than min attribute value
stepMismatch: Bool, // Input does not conform to the step attribute setting
tooLong: Bool, // Input is too long
typeMismatch: Bool, // Wrong input type
valid: Bool, // Input is valid
valueMissing: Bool // Required value is missing
<dd>The form in question has been validated. An object is passed with an object containing a bool, <code>valid</code>, and an array of validity objects corresponding to the validated elements.</dd>
<div class="method">
<a name="addPatterns" />
<dd><p>Take a map of pattern names and HTML5-compatible regular
expressions, and add them to the patternLibrary. Patterns in
the library are automatically assigned to HTML element pattern
attributes for validation.</p>
<dl><dt>{object} patterns</dt><dd><p>A map of pattern names and
HTML5 compatible regular expressions.</p></dd></dl>
<form id="form3" class="h5-defaults">
<label for="phone">Your Phone Number:</label>
<input class="h5-phone" id="phone" name="phone" type="text" placeholder="555-555-5555" title="555-555-5555" />
<script src=""></script>
<p>The <code>class="h5-phone"</code> bit is the part doing the magic. The <code>h5-</code> prefix tells you that this class is a handle that we can use to attach validation rules to. Internally, we just tack this prefix to the front of the pattern names to get the right selectors.</p>
<p>In your JavaScript, specify the pattern name <em>without</em> the class prefix. Keeping the prefix off lets us easily share and re-use pattern libraries between projects.</p>
<script src=""></script>
<dt>$.h5Validate.validValues(selector, values)<dt>
<dd><p>Take a valid jQuery selector, and a list of valid values to
validate against.</p>
<p>If the user input isn't in the list, validation fails.</p>
<dt>{String} selector</dt><dd><p>Any valid jQuery selector.</p></dd>
<dt>{Array} values</dt><dd><p>A list of valid values to validate selected
fields against.</p></dd>
<h2>New Input Types (Use with caution.)</h2>
<div class="announce">
<p><strong>Warning:</strong> Each browser has its own way of treating these new input types. iOS might swap out the on-screen keyboard (cool!), while Chrome renders custom UI controls that don't always make sense (like up and down arrows for <code>datetime</code> inputs.)</p>
<p>What's worse, some of the styles that get applied to these elements are browser-specific, and ignore CSS cascading -- so before you can add your own look and feel, you first have to turn off each native browser's look and feel. For example, h5Validate works just fine on that search field down there, but in Chrome, it ignores our CSS because you first have to turn off <code>-webkit-appearance: searchfield;</code> before you style it. (Hint: You may want to search for a good HTML5 CSS reset). I've left it as is to demonstrate how irritating all of this can be. Good luck, and don't say I didn't warn you.</p>
<p>h5Validate does not currently validate new element types automatically. The <code>pattern</code> and <code>required</code> attributes work fine, but h5Validate <strong>will not</strong> automatically apply email validation to fields with <code>type="email"</code>, etc.. (Yet.)</p>
<p>The plan is to create a thorough testsuite to make sure these validations comply with the html5 standard prior to implementation. So far, it looks like some of the browser implementations are inconsistent. We'll be sharing test cases and filing bug reports in an effort to get all of these validations behaving the same way. Any help with the effort is appreciated. In particular, test cases that show how various implementations differ from the spec would be great. Eventually, we'll defer to the native browser implementation if it's available, with a fall-back to h5Validate's built-in validation rules.</p>
<p>For you bleeding-edge junkies, here are the new input types:</p>
<!-- -->
<form id="newTypes">
<div><input required title="normal" /></div>
<div><input type="tel" required /></div>
<div><input type="search" required /></div>
<div><input type="email" required /></div>
<div><input type="url" required /></div>
<div><input type="datetime" required /></div>
<div><input type="date" required /></div>
<div><input type="month" required /></div>
<div><input type="week" required /></div>
<div><input type="time" required /></div>
<div><input type="datetime-local" required /></div>
<div><input type="number" required /></div>
<div><input type="range" required /></div>
<div><input type="color" required /></div>
<h2>Radio button tests</h2>
<form id="radio">
<input type="radio" name="radio-test" required>1</input>
<input type="radio" name="radio-test" class="checkme" required>2</input>
<input type="radio" name="radio-test" required>3</input>
<script src=""></script>
<script src="jquery.h5validate.js"></script>
<script src=""></script>
<script src="test/test.h5validate.js"></script>
$(document).ready(function () {
phone: /([\+][0-9]{1,3}([ \.\-])?)?([\(][0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/