advanced
   

SocialTwist Tell-a-Friend

Introduction Syntax Placement Variables Operators Popup Boxes If Else Statement Switch Case while Loop for Loop break continue Function Array DOM Events Date String Form Validation

JavaScript : Form / Textbox Validation

There are two types of validation. Server side and Client side. In server side validation the data submitted by the browser, is sent to the server and then the validation is done at the server end. After the validation the result is sent back from server to the browser. Some validation does not require the data to be sent, like blank User ID or Password. Which can be validated at the client side. The advantage is quick response and less load on server. Javascript is used for client side form / textbox validation. On a certain event we can trigger a Javascript function to check the validation of the form.

Lets say, we can check the User Id textbox and Password textbox should not be blank while submitting a login page. For example, here's the markup for a simple HTML page

<html>
<head>
<title>Form / Textbox Validation</title>
<script language="javascript">
function checkValid(){
if(document.forms.frmLogin.txtID.value=="")
{
alert("Please Enter Login ID.");
return false;
}
elseif(document.forms.frmLogin.txtPwd.value=="")
{
alert("Please Enter Password.");
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form name="frmLogin" method="post" action="valid.asp" onsubmit="javascript:return checkValid();">
<h1>Login</h1>
Login ID <input type="text" name="txtID"><br>
Password <input type="password" name="txtPwd"><br>
<input type="submit" name="btnLogin" value="Submit" >
</form>
</body>
</html>
In the above example there is one Javascript custom function to check the validation of the textbox, Which will be fired on the submit event of the form.


Previous
Contact Us
Your Ad Here