23. JavaScript Form Validation

JavaScript Form Validation

Required Fields
E-mail Validation

Form data that typiclly are checked by a javascript
Has the user left required fileds empty ?
Has the user entered a valid e-mail address ?
Has the user entered a valid date ?
Has the user entered text in a numaric field ?

Requited Fields

the following function checks whether a requred filed has been left empty.
If the requred filed is a blank, an alert is displayed, and the function returns false.
If avalue is entered, the function returns true (means that data is ok)

function validate_required(field,alerttxt)
{
with(field)
{
if(value= =null || value= =" ")
{
return false;
alert(alerttext);
}
else
{
return true;
}
}
}

The entire script with the HTML form could look something like this:

<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with(field)
{
if(value= = null || value= = " ")
{
alert(alerttxt)
return false;
}
else
{
return true;
}
}
}

function validate_form(thisform)
{
with(thisform)
{
if(validate_required(email,"Email must be filled out!")==false)
{
email.focus();
return false;
}
}
</script>
</head>
<body>
<form action="submit.htm" onsubmit="return validate_form(this)" method = "post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="submit">
</form>
</body>
</html>

E-mail validation

This means that the input data must contain at least an @ sign and a dot (.).
Also, the @ must not be the first character of the e-mail address,
and the last dot must at lest be one character after the @ sign:

function validate_email(field,alerttext)
{
with(field)
{
if(value.indexOf("@")<1 || valuse.indexOf("@")-value.indexOf(".");
{
alert(alerttext);
return false;
}
else
{
return true;
}
}
}

THe entaire script with the HTML form could look something like this:

<html>
<head>
<script type="text/javascript">
function validate_email(field,alerttext)
{
with(field)
{
if(value.indexOf("@")<1 || value.indexOf("@")-value.indexOf(".")<2)
{
alert(alerttext);
return false;
}
else
{
return true;
}
}
}

function validate_form(thisform)
{
with(thisform)
{
if(validate_email(email,"Not a valid e-mail address!")= =false;)
{
email.focus();
return false;
}
}
}
</script>
</head>
<body>
<form action="submit.htm" onsubmit="return validate_form(this);" method="get">
Email: <input type="text" name="email" size="30">
<input type="submit" value="submit">
</form>
</body>
</html>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams