- Backwards Compatible
- Correct Dynamic Keypad
- Native Validation
- Error Messaging
- User Experience Improvements
- Accessibility
Old Elements
New Elements
<label for="inputID">Label: </label> <input id="inputID" name="inputName" type="text" />
or
<label>Label: <input name="nm" type="text" /></label>
The value of the for attribute should match the value of the input's id attribute
Click on the label to activate the form element:
<label for="telephone">Telephone Number</label> <input type="tel" id="telephone" placeholder="(XXX) XXX-XXXX" aria-describedby="10-digit phone number"> <span class="hint" id="hint">10-digit phone number>/span>10-digit phone number
<form action="#" class="example">
<ul>
<li>
<label for="name">Name</label>
<input type="text" class="classed" required placeholder=" Name" id="name"/>
</li>
<li>
<label for="email">Email</label>
<input type="email" class="classed" multiple placeholder="email addresses" required id="email"/>
</li>
<li>
<label for="dob">Birthday</label>
<input type="date" class="classed" required placeholder="YYYY-MM-DD" id="dob"/>
</li>
<li>
<label for="age">Age</label>
<input type="number" class="classed" required min="21" max="105" id="age"/>
</li>
<li>
<label for="phone">Phone</label>
<input type="tel" class="classed" pattern="\d{3}-\d{3}-\d{4}" placeholder="XXX-XXX-XXXX" id="phone"/>
</li>
<li>
<label for="range">Satisfaction</label>
<input type="range" id="range" min="0" max="10" step="1" list="rangelist" />
</li>
<li>
<label for="tweet">Twitter Handle</label>
<input type="text" class="classed" pattern="@\w{1,18}" placeholder="@twitter_handle" required id="tweet"/>
</li>
<li>
<input type="submit" value="submit">
</li>
</ul>
<datalist id="rangelist">
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</datalist>
</form>
<label for="inputID">Label: </label> <input id="inputID" name="inputName" placeholder="placeholder text" type="text"/>
<label for="inputID">Label: </label>
<input id="inputID" name="inputName"
placeholder="\w{6,9}"
pattern="\w{6,9}"
type="text"/>
(#[a-fA-F0-9]{6})
<label for="inputID">Label: </label>
<input id="inputID" name="inputName"
placeholder="placeholder text"
pattern="\w{6,9}"
required
type="text"/>
input[required] {border: 1px dashed #f00;} or
input:required {border: 1px dashed #f00;}
FF: "Please fill out this field"
UI Pseudo Classes
Attributes instead
input {
background: transparent no-repeat top right;
border: 0 dotted currentColor;
padding: 5px 30px;
}
input:required {
background-image: url('../img/required.png');
}
input:focus:invalid {
background-image: url('../img/invalid.png');
}
input:focus:valid {
background-image: url('../img/valid.png');
}
Note that the following CSS is used in some of our live examples:
input:focus:invalid { background-color: pink;}
input:valid { background-color: lightGreen }
input:required {border: 2px dotted red;}
input:optional {border: 2px solid green;}
input:-webkit-autofill {
background-color: #FAFFBD;
background-image: none;
color: #000;
}
<label for="inputID">Label: </label>
<input id="inputID" name="inputName"
placeholder="placeholder text" pattern="\w{6,9}"
autofocus
required type="text"/>
only one element can have the autofocus attribute
$('[autofocus]').first().focus();
<label for="inputID">Label: </label>
<input id="inputID" name="inputName"
placeholder="placeholder text"
pattern="\w{6,9}"
required
form="notMyParentForm"
type="text" />
Note: form="" disassociates a form element from its parent form.
<label for="inputID">Label: </label>
<input id="inputID" name="inputName"
placeholder="placeholder text"
pattern="\w{6,9}"
required
autofocus
type="text" />
<label for="url">Web Address: </label> <input id="url" type="url" name="url1" placeholder="www.domain.com" required list="datalist1" /> <datalist id="datalist1"> <option value="http://www.standardista.com" /> <option value="http://www.oreilly.com" /> <option value="http://www.evotech.net" /> </datalist>
<input name="color" type="color" />
<input id="color" name="color"
type="color"
placeholder="#FFFFFF"
pattern="#[0-9A-Fa-f]{6}"
required />
<input name="website" type="url" />Any prefix (http:// or even g:// accepted)
<input id="website" name="url" type="url" placeholder="http://www.domain.com" pattern="(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.\/]*"/>
<input name="telephone" type="tel" />
Custom Keyboard. No validation.
<input
id="phone" name="phone" type="tel"
placeholder="415-555-1212"
pattern="\d{3}\-\d{3}\-\d{4}"/>
<input name="address" type="email">
Keyboard changes & validation!
<input id="email" name="email" type="email" placeholder="you@domain.com" multiple />
<input name="n" type="number">
'number/range/date' specific attributes: min, max, step
<input id="nickels" type="number" min="0" max="1000" step="5" placeholder="0, 5, 10 …" />
<input type="number" max="0" step="any" />
<input type="range" min="0" max="10" step="1"/>Current Value:
<input type="range" min="0" max="100" step="5"/>
<input type=range list=lst min=0 max=10> <input type=number list=lst min=0 max=10> <datalist id="lst"> <option>7</option> <option>8</option> <option value=9></option> <option value=10></option> </datalist>
<input type=range value="3,6" multiple />
<input type=range orient="vertical"> // FF
input[type=range]{
width: 20px; height: 200px;
-webkit-appearance: slider-vertical;
writing-mode: bt-lr; /* IE */
}
<input id="search" name="search" type="search">
Note: Dynamic keyboards will show "search" instead of "go"
Warning: if you stylize the input, the 'delete' may not show
<input id="DOB" name="DOB"
type="date"
placeholder="YYYY-MM-DD"
pattern="\d{4}\-\d{2}\-\d{2}"
min="1900-01-01" />
<input name="time" id="time" type="time" min="09:00" max="17:00" step="900" placeholder="12:00" required />
var validityObject = document.getElementById['control'].validity;
function validate() {
var input = document.getElementById('b');
var validityState_object = input.validity;
if(validityState_object.valueMissing) {
input.setCustomValidity('Please set an age (required)');
} else if (input.rangeUnderflow) {
input.setCustomValidity('Your value is too low');
} else if (input.rangeOverflow) {
input.setCustomValidity('Your value is too high');
} else {
input.setCustomValidity('');
}
}
<input type="tel" name="tel"
placeholder="xxx-xxx-xxxx"
title="please enter a phone number with the
pattern of XXX-XXX-XXXX"
pattern="\d{3}\-\d{3}\-\d{4}"
required />
validityObject.patternMismatch
<meter value=1 min=0 max=4 low=1 high=3>1 of 4</meter>
<meter value=61 min=0 max=100 low=73 high=87 optimum=100>D-</meter>
<meter value=90 min=0 max=100 low=20 high=98>75%</meter>
Gas tank:
<meter value=75 min=0 max=100 low=20 high=98>75%</meter>
Grades:
<meter min=50 max=200 low=90 high=119 optimum=100></meter>
Blood Pressure:
<output></output>
<meter value=75 min=0 max=100 >75% full</meter>
Gas tank:
<progress>loading...</progress>
Please wait:
<progress value="75" max="100">75% complete</progress>
Download is: