Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Client-side Validation with Javascript

By Steve Adcock
2005-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>

<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>
Enjoy.

Tutorial Pages:
» Client-side text field validation with Javascript
» Calling the appropriate form
» Amazing Javascript error checking
» Putting it all together


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Essential Javascript -- A Javascript Tutorial
» Submit Forms Conditionally using JavaScript
» How to Setup a Randomising Function