Client-side Validation with Javascript
By Steve Adcock2005-05-14
Putting it all together
Alright, I've broken the code down into sections, so let's bring this code together into one HTML document. Here's what a simple form would look like with three text boxes.
<html>Enjoy.
<head>
<SCRIPT LANGUAGE="javascript">
<!--
function focus()
{
document.forms[0].FirstName.focus();
}
function checkme() //check for required fields
{
if (document.forms[0].FirstName.value == "")
{alert("You did not enter your first name. Please provide it.");
document.forms[0].FirstName.focus();return(false)
}
if (document.forms[0].MiddleName.value == "")
{alert("You did not enter your middle name. Please provide it.");
document.forms[0].MiddleName.focus();return(false)
}
if (document.forms[0].LastName.value == "")
{alert("You did not enter your last name. Please provide it.");
document.forms[0].LastName.focus();return(false)
}
}
//-->
</SCRIPT>
</head>
<body onLoad="focus()">
<form action=test.php method=post onSubmit="return checkme()" name=Feedback>
First name: <input type="text" name="FirstName"><br>
Middle name: <input type="text" name="MiddleName"><br>
Last name: <input type="text" name="LastName"><br>
<input type="Submit">
</form>
</body>
</html>
Tutorial Pages:
» Client-side text field validation with Javascript
» Calling the appropriate form
» Amazing Javascript error checking
» Putting it all together
