Web Programming
Javascript
1 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Agenda
1 JavaScript Validations
2 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Objectives
At the end of this module you will be able to:
• Validate user inputs through javascript functions
3 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
JavaScript Validations
4 © 2012 WIPRO LTD | [Link]
Javascript Validation Example 1
• The following example demonstrates how you can define a
function that is going to ensure that the text box cannot remain
empty :
<html>
<head>
<title>Handling Form Data</title>
<script>
function chkfrm(){
var x=[Link];
if([Link]==0){
alert("Please Enter Name \n");
return false;
}
Contd..
5 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Javascript Validation Example 1 (Contd.).
else{
return true;
}
}
</script></head>
<body>
<form name ="card" method=post action="Hello"
onsubmit="return chkfrm();">
<center>
<B> Enter Your Name : </B>
<p><input type="text" name="name" >
<p><input type="submit" value="submit">
</center>
</form></body>
</html>
6 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Javascript Validation Example 2
• The following example demonstrates how you can define a
function that is going to ensure that exactly 10 digits are
entered in the text box :
<html>
<head><script>
function chkfrm(){
var flag=0;
var x=[Link];
if([Link]!=10){
if(!isNaN(x))
alert("You are expected to enter 10
digits");
}
else
flag = flag + 1; Contd..
7 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Javascript Validation Example 2 (Contd.).
if(isNaN(x))
alert("Only digits nothing else");
else
flag = flag+1;
if(flag==2)
return true;
else
return false; }
</script></head><body>
<form name ="card" method=post onsubmit="return
chkfrm();">
<center><B> Enter Your Mobile No(exactly 10 digits) :
</B>
<p><input type="text" name=“mob" >
<p><input type="submit" value="submit"></center>
</form></body>
</html>
8 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Javascript Validation Example 3
• The following example demonstrates how you can define a function
that is going to ensure that only alphabets(lower as well as upper
case) and space(keycode=32) can be entered in the text box :
<script language="Javascript">
function ValidateAlpha(){
var keyCode = [Link];
if ((keyCode < 65 || keyCode > 90) && (keyCode < 97
|| keyCode > 123) && keyCode != 32){
[Link] = false;
alert("Only alphabets or space..please");
}
}
</script>
Contd..
9 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Javascript Validation Example 3(Contd.).
<form method=“post” action=“xyz” >
Enter your Name :
<input type="text" id="txtBox"
onKeyPress="ValidateAlpha();" />
</form>
10 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Summary
In this module, you were able to:
• Validate user inputs through javascript functions
11 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL
Thank You
12 © 2012 WIPRO LTD | [Link] | CONFIDENTIAL