Breaking News
yeni film izle hd film seyret film izle

Validation Using JavaScript

We can validate the user input for our form inquiries using JavaScript. A form controls are used to collect data from the user , but we also know that garbage in garbage out, if a bad data will be collected then the results will also be bad. So to collect a good data we require validations & checks on the data being collected. Like if name of the person is asked the user can enter only name only in the text field. There are some examples to validate name, age, phone number of a person.
 
Example to Validate a user’s name:
 
<html>

<head>

</head>

<body bgcolor=yellow text=red>

Name :<input type=”text” name=”t1″>

<input type=”Button” value=”check” onclick=”ncheck()”>

<script>

var ok=0

function ncheck()

{

var a=t1.value;

for(i=0;i<a.length;i++)

{

if((a.charAt(i)>=’a’ && a.charAt(i)<=’z’)||(a.charAt(i)>=’A’ && a.charAt(i)<=’Z’) || (a.charAt==” ” ))

{

ok=1

}

else

{

ok=0

}

}

if (ok==0)

{

t1.value=”Invalid Name”;

}

else

{

t1.value=”Good Name”;

}

}

</script>

</body>

</html>

Click here to view the result of this program on browser
 
 
Example to validate a user’s age:
 
<html>

<head>

</head>

<body bgcolor=yellow text=red>

How many years old are Your ? :<input type=”text” name=”t1″>

<input type=”Button” value=”check” onclick=”ncheck()”>

 

<script>

var ok=0

function ncheck()

{

var a=t1.value;

should=0

if(a.length>0 && a.length<3)

{ should=1 }

if (a.length>0 && parseInt(a.value)==0)

{

should =0 }

if (should==1)

{

for(i=0;i<a.length;i++)

{

if((a.charAt(i)>0) && (a.charAt(i)<10))

{

ok=1;

}

else

{ ok=0

break; }

}

}

if ((ok==1) && (should==1))

{ t1.value=”..Long Live…”; }

else

{ t1.value=”Do not play.. please..”; }

}

</script>

</body>

</html>

Click here to view the result of this program on browser
 
 
Example to validate a user’s Phone number:
 
<html>

<head>

</head>

<body bgcolor=yellow text=red>

Enter Phone Number :<input type=”text” name=”t1″>

<input type=”Button” value=”check” onclick=”ncheck()”>

 

<script>

var ok=0

function ncheck()

{

var a=t1.value;

should=0

if(a.length==8)

{ should=1 }

if (a.length>9 && a.length <12)

{ should=1 }

 

if (should==1)

{

for(i=0;i<a.length;i++)

{

if((a.charAt(i)>-1) && (a.charAt(i)<10))

{

 

ok=1;

}

else

{ ok=0

break; }

}

}

else

{ t1.value=”Invalid Phone Number”; }

 

if ((ok==1) && (should==1))

{ t1.value=”valid Phone Number”; }

else

{ t1.value=”Invalid Phone Number”; }

}

</script>

</body>

</html>

Check Also

Functions Returning Values In Java Script

Function is a set of statements recognized by a name and can be used as …