Hi Friends,
Welcome back to the Blog.... Please note these 'validations' in your Observations and Practice them in WT lab tomorrow..
1) For the Fields Like NAME, which should not be left blank.. the following validation has to be done.
<html>
<head>
<script type="text/javascript">
function validateform()
{
varx=document.forms["myForm"]["fname"].value;
if(x==null || x==" ")
{
alert("First Name Must be Filled");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp"
onsubmit="return validateform();" method="post">
First Name:<input type="text" name="fname">
<input type="submit" value="submit">
</form>
</body>
</html>
2)For The fields where spaces are not allowed: // this is a function and should be used in scripts in head section
var ichas=" ";
for(var i=0;i<document.forms["myForm"]["fname"].value.length;i++)
{
if(ichas.Indexof(document.forms["myForm"]["fname"].value.charAt(0))!=-1)
{
alert("Spaces are Not allowed");
return false;
}
}
3)EMail Validation:
<html>
<head>
<script type=text/javascript>
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.IndexOf("@");
var dotpos=x.lastIndexOf(".");
if(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a Valid E-Mail Address");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp"
onsubmit="return validateform();" method="post">
E Mail: <input type="text" name="email">
<input type="submit" value="submit">
</form>
</body>
</html>
// these are the functions to be used with the scripts in the head section
4) Name to be only in alphabets only
var mychar1=/^[a-ZA-z]+$/;
if(!mychar1.test(document.forms["myForm"]["fname"].value))
{
alert("Enter name in alphabets only");
}
5) To have a fixed length:
if(fname.length<6)
{
alert("Enter fname minimum 6 characters");
return false;
}
if(fname.length>25)
{
alert("Enter Name with maximum of 25 characters");
return false;
}
6) Validating a Mobile Number:
var contact=document.forms["myForm"]["mobileNo"].value;
if(contact==null || contact==" ")
{
alert("Enter Mobile Number");
return false;
}
7) For a Field to be Numericals only:
var digits="0123456789";
var temp;
for(var i=0;i<document.forms["myForm"]["mobileno"].value.length;i++)
{
temp=document.forms["myForm"]["mobileno"].value.substring(i,i+1);
if(digits.IndexOf(temp)==-1)
{
alert("please Enter numbers only for mobile number");
return false;
}
}
8) For a Field to have a restricted length:
if(contact.length<10)
{
alert("Enter 10 digits only for Mobile Number");
return false;
}
Welcome back to the Blog.... Please note these 'validations' in your Observations and Practice them in WT lab tomorrow..
1) For the Fields Like NAME, which should not be left blank.. the following validation has to be done.
<html>
<head>
<script type="text/javascript">
function validateform()
{
varx=document.forms["myForm"]["fname"].value;
if(x==null || x==" ")
{
alert("First Name Must be Filled");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp"
onsubmit="return validateform();" method="post">
First Name:<input type="text" name="fname">
<input type="submit" value="submit">
</form>
</body>
</html>
2)For The fields where spaces are not allowed: // this is a function and should be used in scripts in head section
var ichas=" ";
for(var i=0;i<document.forms["myForm"]["fname"].value.length;i++)
{
if(ichas.Indexof(document.forms["myForm"]["fname"].value.charAt(0))!=-1)
{
alert("Spaces are Not allowed");
return false;
}
}
3)EMail Validation:
<html>
<head>
<script type=text/javascript>
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.IndexOf("@");
var dotpos=x.lastIndexOf(".");
if(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a Valid E-Mail Address");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp"
onsubmit="return validateform();" method="post">
E Mail: <input type="text" name="email">
<input type="submit" value="submit">
</form>
</body>
</html>
// these are the functions to be used with the scripts in the head section
4) Name to be only in alphabets only
var mychar1=/^[a-ZA-z]+$/;
if(!mychar1.test(document.forms["myForm"]["fname"].value))
{
alert("Enter name in alphabets only");
}
5) To have a fixed length:
if(fname.length<6)
{
alert("Enter fname minimum 6 characters");
return false;
}
if(fname.length>25)
{
alert("Enter Name with maximum of 25 characters");
return false;
}
6) Validating a Mobile Number:
var contact=document.forms["myForm"]["mobileNo"].value;
if(contact==null || contact==" ")
{
alert("Enter Mobile Number");
return false;
}
7) For a Field to be Numericals only:
var digits="0123456789";
var temp;
for(var i=0;i<document.forms["myForm"]["mobileno"].value.length;i++)
{
temp=document.forms["myForm"]["mobileno"].value.substring(i,i+1);
if(digits.IndexOf(temp)==-1)
{
alert("please Enter numbers only for mobile number");
return false;
}
}
8) For a Field to have a restricted length:
if(contact.length<10)
{
alert("Enter 10 digits only for Mobile Number");
return false;
}
2 comments:
For any further classifications and explanations in the code you can contact our srujana mam tomorrow..
emina artham ithe kada doubts raniki....
Post a Comment