Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Sensational Exposé with Bewildering Demonstra...
Search
Peter Gasston
April 18, 2011
Programming
1
50
A Sensational Exposé with Bewildering Demonstrations!
On the HTML5 Constraint Validation API
Peter Gasston
April 18, 2011
Tweet
Share
More Decks by Peter Gasston
See All by Peter Gasston
People Don’t Change
stopsatgreen
0
120
Your Reality Here
stopsatgreen
0
75
Growing Up, Getting Serious - #SotB6
stopsatgreen
1
310
Growing Up, Getting Serious
stopsatgreen
0
65
Surveying the Landscape — November 2016
stopsatgreen
1
130
Surveying the Landscape - Fronteers
stopsatgreen
2
440
The Web vs. The Browser
stopsatgreen
0
150
Surveying the Landscape Sept. 2016
stopsatgreen
1
260
Surveying the Landscape
stopsatgreen
4
710
Other Decks in Programming
See All in Programming
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Contemporary Test Cases
maaretp
0
140
Quine, Polyglot, 良いコード
qnighy
4
650
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
最新TCAキャッチアップ
0si43
0
200
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
360
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
距離関数を極める! / SESSIONS 2024
gam0022
0
300
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Bash Introduction
62gerente
608
210k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
What's in a price? How to price your products and services
michaelherold
243
12k
The Cult of Friendly URLs
andyhume
78
6k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Practical Orchestrator
shlominoach
186
10k
How to Ace a Technical Interview
jacobian
276
23k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Transcript
A Sensational ExposÉ With Bewildering Demonstrations By Peter Gasston Broken-links.com
@stopsatgreen
None
HARRY HOUDINI
None
None
None
None
CONSTRAINT validation
CONSTRAIN: 1. Severely restrict scope, extent, or activity 2. confine
forcibly, as by bonds.
None
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" "> >
None
NEW INPUT TYPES <input type= <input type=" "number number" ">
> <input type= <input type=" "range range" "> >
None
NEW JS Methods stepUp()/stepDown() stepUp()/stepDown() valueAsNumber valueAsNumber NEW JS FUNCTION
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" "> >
None
NEW JS FUNCTION valueAsDate valueAsDate
wufoo.com/html5/
ADDING THE CONSTRAINTs
REQUIRED ATTRIBUTE < <input type= input type=" "text text" "
required required> > < <input type="text" input type="text" required required= ="" ""> > < <textarea textarea required required= ="" "" /> />
REQUIRED ATTRIBUTE
SET THE ERROR MESSAGE < <input type= input type=" "text
text" " x-moz-errormessage x-moz-errormessage ="FooBar"> ="FooBar">
SET THE ERROR MESSAGE
Pattern matching <input type= <input type=" "email email" "> >
<input type= <input type="date" "date"> > <input type= <input type=" "url url" " required> required>
Pattern matching
Pattern ATTRIBUTE < <input type= input type=" "text text" "
pattern="[-0-9]+" pattern="[-0-9]+"> >
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"> >
None
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"> >
None
None
None
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
CONSTRAINT VALIDATION A.P.I.
Is the api present? var hasValidation = var hasValidation =
(myForm. (myForm.checkValidity checkValidity) ) ? true : false; ? true : false;
None
Do not validate < <form form novalidate> novalidate> < <button
button formnovalidate> formnovalidate>
Do not validate if(hasValidation === true) { if(hasValidation === true)
{ myForm. myForm. setAttribute(' setAttribute('novalidate novalidate',''); ',''); } }
Feature detection function formSubmit(evt) { function formSubmit(evt) { (hasValidation ===
true) ? (hasValidation === true) ? newValidation() : newValidation() : oldValidation(); oldValidation(); evt.preventDefault(); evt.preventDefault(); } }
BONUS SLIDe! HTML5 Form Events: HTML5 Form Events: oncontextmenu oncontextmenu
onformchange onformchange onforminput onforminput oninput oninput oninvalid oninvalid
DOES THE FORM VALIDATE? myForm. myForm.checkValidity(); checkValidity();
document.getElementById document.getElementById ('x'). ('x').willValidate; willValidate; Which ELEMENTs will VALIDATE?
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; ; } } } }
DOES A field VALIDATE? thisNode. thisNode. checkValidity(); checkValidity();
Validation status thisNode thisNode. .validity; validity;
Validity object customError customError patternMismatch patternMismatch rangeOverflow rangeOverflow rangeUnderflow rangeUnderflow
stepMismatch stepMismatch tooLong tooLong typeMismatch typeMismatch valid valid valueMissing valueMissing
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; } } } }
SET THE ERROR MESSAGE thisNode. thisNode. setCustomValidity setCustomValidity (errorMessage); (errorMessage);
GET THE ERROR MESSAGE document. document. getElementById('x'). getElementById('x'). validationMessage; validationMessage;
alert('The error message is: ' alert('The error message is: '
+ thisNode. + thisNode.validationMessage validationMessage); );
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
ESCAPING THE CONSTRAINTs
THE END
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
THANK You. By Peter Gasston Broken-links.com @stopsatgreen