Form validation
Written by: maffelu , 2009-10-16 10:20:42
<form action="validateForm1.html" method="post" name="myForm" onsubmit="return validateForm();">
<table>
<tr>
<th>Name</th>
<td><input type="text" name="name" id="name" /></td>
<td><p id="nameError" class="error"></p></td>
</tr>
<tr>
<th>Email</th>
<td><input type="text" name="mail" id="mail" /></td>
<td><p id="mailError" class="error"></p></td>
</tr>
<tr>
<th>Age</th>
<td><input type="text" name="age" id="age" /></td>
<td><p id="ageError" class="error"></p></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form>
var isNumeric = /^[0-9]+$/;
var isLetters = /^[a-zA-Z]+$/;
var isEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var nameControl = document.getElementById("name");
var mailControl = document.getElementById("mail");
var ageControl = document.getElementById("age");
var nameError = document.getElementById("nameError");
var mailError = document.getElementById("mailError");
var ageError = document.getElementById("ageError");
if(nameControl.value == "")
nameError.innerHTML = " * Name is empty";
else if(nameControl.value.search(isLetters))
nameError.innerHTML = " * Name must only contain letters";
else
nameError.innerHTML = "";
if(nameError.innerHTML == "" && mailError.innerHTML == "" && ageError.innerHTML == "")
return true;
else
return false;
<html>
<head>
<script type="text/javascript">
function validateForm() {
//Get the controls
var nameControl = document.getElementById("name");
var mailControl = document.getElementById("mail");
var ageControl = document.getElementById("age");
//Get the error spans
var nameError = document.getElementById("nameError");
var mailError = document.getElementById("mailError");
var ageError = document.getElementById("ageError");
//Create expressions
var isNumeric = /^[0-9]+$/;
var isLetters = /^[a-zA-Z]+$/;
var isEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
//Cheack each one and if it fails, set an appropriate error message
//If name is empty or invalid
if(nameControl.value == "")
nameError.innerHTML = " * Name is empty";
else if(nameControl.value.search(isLetters))
nameError.innerHTML = " * Name must only contain letters";
else
nameError.innerHTML = "";
//If mail is empty or invalid
if(mailControl.value == "")
mailError.innerHTML = " * Email is empty";
else if(mailControl.value.search(isEmail))
mailError.innerHTML = " * Email is not valid";
else
mailError.innerHTML = "";
//If age is empty or invalid
if(ageControl.value == "")
ageError.innerHTML = " * Age is empty";
else if(ageControl.value.search(isNumeric))
ageError.innerHTML = " * Age must only contain numbers";
else
ageError.innerHTML = "";
//If any errors occurred, return false, otherwise true
if(nameError.innerHTML == "" && mailError.innerHTML == "" && ageError.innerHTML == "")
return true;
else
return false;
};
</script>
<style type="text/css">
.error {
color: #ff0000;
font-weight: bold;
}
</style>
<title>A simple form validation example</title>
</head>
<body>
<form action="validateForm1.html" method="post" name="myForm" onsubmit="return validateForm();">
<table>
<tr>
<th>Name</th>
<td><input type="text" name="name" id="name" /></td>
<td><p id="nameError" class="error"></p></td>
</tr>
<tr>
<th>Email</th>
<td><input type="text" name="mail" id="mail" /></td>
<td><p id="mailError" class="error"></p></td>
</tr>
<tr>
<th>Age</th>
<td><input type="text" name="age" id="age" /></td>
<td><p id="ageError" class="error"></p></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
There are 5 comments on this article.
Balu
2010-12-10 06:57:38
Good one.. helped me much. thnx
666
2011-03-07 03:51:50
55
Malti
2011-03-07 03:55:03
Can you please let me know the javascript for the following functionality-
I want the error messages to appear as a popup as soon as i fill in an incorrect entry in a textbox of a form in a webpage and not after clicking on the "submit button" or "next page" button. Please Help!!
fgsd
2011-07-02 03:33:09
g
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
dfd
2010-11-03 12:18:49
fff