Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Sensational Exposé with Bewildering Demonstra...

A Sensational Exposé with Bewildering Demonstrations!

On the HTML5 Constraint Validation API

Peter Gasston

April 18, 2011
Tweet

More Decks by Peter Gasston

Other Decks in Programming

Transcript

  1. NEW INPUT TYPES <input type= <input type=" "email email" ">

    > <input type= <input type=" "url url" "> > <input type= <input type=" "tel tel" "> > <input type= <input type=" "search search" "> >
  2. NEW INPUT TYPES <input type= <input type=" "number number" ">

    > <input type= <input type=" "range range" "> >
  3. NEW INPUT TYPES <input type= <input type=" "datetime datetime" ">

    > <input type= <input type=" "date date" "> > <input type= <input type=" "month month" "> > <input type= <input type=" "week week" "> > <input type= <input type=" "time time" "> >
  4. REQUIRED ATTRIBUTE < <input type= input type=" "text text" "

    required required> > < <input type="text" input type="text" required required= ="" ""> > < <textarea textarea required required= ="" "" /> />
  5. SET THE ERROR MESSAGE < <input type= input type=" "text

    text" " x-moz-errormessage x-moz-errormessage ="FooBar"> ="FooBar">
  6. Pattern matching <input type= <input type=" "email email" "> >

    <input type= <input type="date" "date"> > <input type= <input type=" "url url" " required> required>
  7. Pattern ATTRIBUTE < <input type= input type=" "text text" "

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

    " pattern="[-0-9]+" pattern="[-0-9]+" title="Must be a title="Must be a number" number"> >
  9. LIMIT MAtching < <input type= input type="number" "number" max="2" max="2">

    > < <input type= input type="range" "range" min="0" min="0" max="1" step="0.1" max="1" step="0.1"> > < <input type= input type="text" "text" maxlength="20" maxlength="20"> >
  10. BONUS SLIDE! CSS3 UI pseudo-classes for CSS3 UI pseudo-classes for

    styling form states: styling form states: :required :required :optional :optional :valid :valid :invalid :invalid :default :default :in-range :in-range :out-of-range :out-of-range
  11. Is the api present? var hasValidation = var hasValidation =

    (myForm. (myForm.checkValidity checkValidity) ) ? true : false; ? true : false;
  12. Do not validate if(hasValidation === true) { if(hasValidation === true)

    { myForm. myForm. setAttribute(' setAttribute('novalidate novalidate',''); ',''); } }
  13. Feature detection function formSubmit(evt) { function formSubmit(evt) { (hasValidation ===

    true) ? (hasValidation === true) ? newValidation() : newValidation() : oldValidation(); oldValidation(); evt.preventDefault(); evt.preventDefault(); } }
  14. BONUS SLIDe! HTML5 Form Events: HTML5 Form Events: oncontextmenu oncontextmenu

    onformchange onformchange onforminput onforminput oninput oninput oninvalid oninvalid
  15. Which ELEMENTs will VALIDATE? var formNodes = var formNodes =

    myForm.childNodes.length; myForm.childNodes.length; for(i=0; i<formNodes; i++) for(i=0; i<formNodes; i++) { { if(myForm.childNodes[i] if(myForm.childNodes[i]. . willValidate willValidate) { ) { doSomething doSomething; ; } } } }
  16. Validity object customError customError patternMismatch patternMismatch rangeOverflow rangeOverflow rangeUnderflow rangeUnderflow

    stepMismatch stepMismatch tooLong tooLong typeMismatch typeMismatch valid valid valueMissing valueMissing
  17. Validity object for(var v in thisNode.validity) { for(var v in

    thisNode.validity) { if (thisNode. if (thisNode.validity validity[v] === true) { [v] === true) { var errorMessage; var errorMessage; switch(v){ switch(v){ case 'valueMissing' : case 'valueMissing' : errorMessage = 'FooBar'; errorMessage = 'FooBar'; break; break; } } } }
  18. alert('The error message is: ' alert('The error message is: '

    + thisNode. + thisNode.validationMessage validationMessage); );
  19. BONUS SLIDE! Javascript library Javascript library to polyfill HTML5 to

    polyfill HTML5 form support: form support: http://thecssninja.c http://thecssninja.c om/javascript/H5F om/javascript/H5F
  20. BONUS SLIDE! Resources: Resources: http://www.broken- http://www.broken- links.com/2011/03/28/html links.com/2011/03/28/html 5-form-validation/ 5-form-validation/

    https://developer.mozilla https://developer.mozilla .org/en/HTML/HTML5/Forms_ .org/en/HTML/HTML5/Forms_ in_HTML5 in_HTML5