Microsoft Official Course: Creating Forms To Collect and Validate User Input
Microsoft Official Course: Creating Forms To Collect and Validate User Input
Module 4
• autofocus
• autocomplete
• required
• pattern
• placeholder
• many other input type-specific attributes
Lesson 2: Validating User Input by Using HTML5
Attributes
• Principles of Validation
• Ensuring that Fields are Not Empty
• Validating Numeric Input
• Validating Text Input
• Styling Fields to Provide Feedback
Principles of Validation
input {
border: solid 1px;
}
input:invalid {
border-color: #f00;
}
input:valid {
border-color: #0f0;
}
Lesson 3: Validating User Input by Using JavaScript
function validateForm()
{
if( ! isAnInteger(document.getElementById('score').value))
return false; /* No, it’s not a number! Form validation fails */
return true;
}
Ensuring that Fields are Not Empty
Use JavaScript code to ensure that a required field does not contain
only whitespace:
function validateForm() {
if( ! isSignificant(document.getElementById(‘penalties’).value))
return false; /* No! Form validation fails */
return true;
}
Providing Feedback to the User
if( ! isSignificant(textBox.value)) {
textbox.className = "validationError";
return false; /* No! Form validation fails */
}
textbox.className = "validatedFine";
return true;
}
Demonstration: Creating a Form and Validating
User Input
Logon Information
• Virtual Machines: 20480B-SEA-DEV11, MSL-TMG1
• User Name: Student
• Password: Pa$$w0rd
• Review Question(s)