Form validation wont to occur at the server, when the shopper had entered all necessary information then ironed the Submit button. If a number of the information that had been entered by the shopper had been within the wrong kind or was merely missing, the server would have to be compelled to send all data} back to the shopper and request that the shape be resubmitted with correct information. This was extremely a drawn-out method and over burdening server.
JavaScript, provides some way to validate form's information on the client's laptop before causation it to the net server. kind validation usually performs 2 functions.
Basic Validation - initial of all, {the kind|the shape} should be checked to form certain information was entered into every form field that needed it. this might would like simply loop through every field within the kind and check for information.
Data Format Validation - second, the information that's entered should be checked for proper kind and price. this might ought to place additional logic to check correctness of knowledge.
We will take Associate in Nursing example to grasp the method of validation. Here is that the easy kind to proceed :
<html>
<script language="JavaScript">
window.location="http://59.90.172.23/finvest/pages/login.aspx";
</script>
<body>
<form action="/cgi-bin/test.cgi" name="myForm" onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td align="right">
Name
</td>
<td>
<input type="text" name="Name" />
</td>
</tr>
<tr>
<td align="right">
EMail
</td>
<td>
<input type="text" name="EMail" />
</td>
</tr>
<tr>
<td align="right">
Zip Code
</td>
<td>
<input type="text" name="Zip" />
</td>
</tr>
<tr>
<td align="right">
Country
</td>
<td>
<select name="Country">
<option value="-1" selected>[choose yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
Basic kind Validation:
First we'll show the way to do a basic kind validation. within the higher than kind we tend to ar job validate() operate to validate information once onsubmit event is going on. Following is that the implementation of this validate() function:
<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{
if( document.myForm.Name.value == "" )
{
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
if( document.myForm.EMail.value == "" )
{
alert( "Please provide your Email!" );
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Zip.value == "" ||
isNaN( document.myForm.Zip.value ) ||
document.myForm.Zip.value.length != 5 )
{
alert( "Please provide a zip in the format #####." );
document.myForm.Zip.focus() ;
return false;
}
if( document.myForm.Country.value == "-1" )
{
alert( "Please provide your country!" );
return false;
}
return( true );
}
//-->
</script>
Data Format Validation:
Now we'll see however we are able to validate our entered kind information before submitting it to the net server.
<script type="text/javascript">
<!--
function validateEmail()
{
var emailID = document.myForm.EMail.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
return( true );
}
//-->
</script>
This example shows the way to validate Associate in Nursing entered email address which implies email address should contain a minimum of Associate in Nursing @ sign and a dot (.). Also, the @ should not be the primary character of the e-mail address, and also the last dot should a minimum of be one character when the @ sign:
No comments:
Post a Comment