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>
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:
Post a Comment