Javascript Forms Part III
Javascript Forms Part III
FORM VALIDATION
Working with Forms
Handling Forms Recap
• Add submit handler to form itself
• Always have a submit button
• Always use valid action attribute
• HTML5 supports browser-based form
validation
JavaScript – Validate Forms Data
Provides an ID by
which the field can
be referred
Radio Buttons
<select>
<option>text here</option>
<option>other text here</option>
</select>
Selection Forms and Drop-Down Lists
• There are three attributes of interest
• Reset buttons are simply buttons which restore the input fields
on your form to their default setting
• Attributes
required user needs to complete field for form to be able to submit
not supported in Internet Explorer 9- or in Safari
/* If field value is empty, return text. If field is not empty, return empty string
// use errorString as function parameterso that we can use same function for any
field */
function fieldEmpty (fieldvalue, errorString) {
if (fieldvalue == "") {
return (errorString);
} else {
return ""; // return empty string
}
}
Ensuring Forms are Not Blank
<html>
<head>
<script type='text/javascript'>
function leftBlank(formElement, warning){
if(formElement.value.length == 0){
alert(warning);
}
}
</script>
</head>
<body> <form>
<input type='text' id='customer name'/>
<input type='button'
onclick="leftBlank(document.getElementById('customer name'), 'Enter name')"
value='Check Field' />
</form> </body> </html>
Function to Test if Radio Button is Selected
<script type='text/javascript'>
function isSelection(elem, message){
if(elem.value == "Select a gender"){
alert(message);
}
}
</script>
</head>
<body> <form>
<select id='selection'>
<option>Select a gender</option>
<option>Male</option>
<option>Female</option>
</select>
<input type='button‘ onclick="isSelection(document.getElementById('selection'),
'Choose Male or Female')" value='Check Field' />
</form> </body> </html>
Restricting the Length of Input
<html>
<head>
<script type='text/javascript'>
function ensureLength(elem, min, max, message){
var userName = elem.value;
if(userName.length < min || userName.length > max){
alert(message);
}
}
</script>
</head>
<body>
<form> <input type='text' id='user name'/>
<input type='button'
onclick="ensureLength(document.getElementById('user name'), 4, 8, 'User name must be
between 4 and 8 characters')"
value='Enter a user name' />
</form>
</body>
</html>
Function to Test if Number is Within Certain
Range
function validateAge(fieldvalue) {
if (isNaN(fieldvalue)) return "No Age was entered.\n"
else if (fieldvalue < 18 || fieldvalue > 110)
return "Age must be between 18 and 110.\n"
return ""
}
Ensuring Input is Alphabetic
<html>
<head>
<script type='text/javascript'>
function isNumeric(elem, message){
var alphabeticExpression = /^[a-zA-Z]+$/;
/* This is a a regular expression /^[0-9]+$/ that will only match if the
string is all letters and is at least one character long */
if(!elem.value.match(alphabeticExpression )){
alert(message);
}
}
</script>
</head>
<body> <form> <input type='text' id='country'/>
<input type='button'
onclick="isNumeric(document.getElementById('country'), 'Not a country')"
value='Enter the country of the shipping address' />
<input type='button'
onclick="isNumeric(document.getElementById('credit card'), 'Not valid card')"
value='Enter credit card number' />
</form> </body> </html>
Ensuring Input is Alphanumeric
<html>
<head>
<script type='text/javascript'>
function isAlphaNumeric(elem, message){
var alphaNumericExpression = /[0-9a-zA-Z]/;
/* This is a regular expression /[0-9a-zA-Z]/ that will only match if the
string contains either letters or numbers, and is at least one character long */
if(!elem.value.match(alphaNumericExpression )){
alert(message);
}
}
</script>
</head>
<body> <form> <input type='text' id='street'/>
<input type='button'
onclick="isAlphaNumeric(document.getElementById('street'), 'Include the street
name and number')“ value='Enter the street of the shipping address' />
</form> </body> </html>
Function to Test Email Address
function validateEmail (fieldvalue) {
if (fieldvalue == "") return "No Email was entered.\n"
|| // Boolean OR
/[^a-zA-Z0-9.@_-]/.test(fieldvalue)
)
return "The Email address is invalid.\n"
return ""
}
Validating an Email Address
<html>
<head>
<script type='text/javascript'>
function emailValidator(elem, Message){
var emailExpression = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
/* This expression checks to ensure the input follows the syntax of
[email protected]*/
if(!elem.value.match(emailExpression)){
alert(Message);
}
}
</script>
</head>
<body> <form> <input type='text' id='emailer'/>
document.getElementById('submitButton').disabled = 'disabled';
OR
document.getElementById('submitButton').disabled = true;
Getting Values
var data = document.getElementById('comments').value;
if (which.checked) {
}
Exercise