A Sensational
  ExposÉ With
  Bewildering
Demonstrations



     By Peter Gasston
    Broken-links.com
      @stopsatgreen
HARRY HOUDINI
CONSTRAINT
validation
CONSTRAIN:
1. Severely restrict
scope, extent,
or activity

2. confine forcibly,
as by bonds.
NEW INPUT TYPES

<input   type="email">
<input   type="url">
<input   type="tel">
<input   type="search">
NEW INPUT TYPES


<input   type="number">
<input   type="range">
NEW JS Methods
stepUp()/stepDown()

NEW JS FUNCTION

valueAsNumber
NEW INPUT TYPES

<input   type="datetime">
<input   type="date">
<input   type="month">
<input   type="week">
<input   type="time">
NEW JS FUNCTION

valueAsDate
wufoo.com/html5/
ADDING THE
CONSTRAINTs
REQUIRED
     ATTRIBUTE

<input type="text" required>
<input type="text"
required="">
<textarea required="" />
REQUIRED
ATTRIBUTE
SET THE
ERROR MESSAGE

<input type="text"
x-moz-errormessage
="FooBar">
SET THE
ERROR MESSAGE
Pattern
      matching

<input type="email">
<input type="date">
<input type="url" required>
Pattern
matching
Pattern
 ATTRIBUTE

<input type="text"
pattern="[-0-9]+">
CONTEXTUAL
ERROR MESSAGES

 <input type="text"
 pattern="[-0-9]+"
 title="Must be a
 number">
LIMIT
      MAtching
<input type="number" max="2">
<input type="range" min="0"
max="1" step="0.1">
<input type="text"
maxlength="20">
BONUS SLIDE!
CSS3 UI pseudo-classes for
styling form states:
:required
:optional
:valid
:invalid
:default
:in-range
:out-of-range
CONSTRAINT
VALIDATION
   A.P.I.
Is the api
     present?

var hasValidation =
(myForm.checkValidity)
? true : false;
Do not
    validate

<form novalidate>
<button formnovalidate>
Do not
       validate

if(hasValidation === true) {
  myForm.
setAttribute('novalidate','');
}
Feature
      detection
function formSubmit(evt) {
  (hasValidation === true) ?
  newValidation() :
  oldValidation();
  evt.preventDefault();
}
BONUS SLIDe!

HTML5 Form Events:
oncontextmenu
onformchange
onforminput
oninput
oninvalid
DOES THE FORM
   VALIDATE?

myForm.checkValidity();
Which ELEMENTs
will VALIDATE?

document.getElementById
('x').willValidate;
Which ELEMENTs
will VALIDATE?
var formNodes =
myForm.childNodes.length;
for(i=0; i<formNodes; i++) {
  if(myForm.childNodes[i].
willValidate) {
    doSomething;
  }
}
DOES A field
   VALIDATE?

thisNode.
checkValidity();
Validation
   status


thisNode.validity;
Validity object

   customError
   patternMismatch
   rangeOverflow
   rangeUnderflow
   stepMismatch
   tooLong
   typeMismatch
   valid
   valueMissing
Validity
         object
for(var v in thisNode.validity) {
if (thisNode.validity[v] === true) {
  var errorMessage;
  switch(v){
    case 'valueMissing' :
    errorMessage = 'FooBar';
    break;
  }
}
SET THE
ERROR MESSAGE

thisNode.
setCustomValidity
(errorMessage);
GET THE
ERROR MESSAGE

document.
getElementById('x').
validationMessage;
alert('The error message is: '
+ thisNode.validationMessage);
BONUS SLIDE!

Javascript library
to polyfill HTML5
form support:
http://thecssninja.c
om/javascript/H5F
ESCAPING THE
CONSTRAINTs
THE END
BONUS SLIDE!
Resources:
http://www.broken-
links.com/2011/03/28/html
5-form-validation/

https://developer.mozilla
.org/en/HTML/HTML5/Forms_
in_HTML5
By Peter Gasston
  Broken-links.com
    @stopsatgreen




THANK You.

A Sensational Exposé With Bewildering Demonstrations