Validating, Errors, Debugging, Exception Handling, and Security
Validating Forms
1.Required fields
2.Numbers
3.User name and password
4. Phone number
5. Time
6. Date
7. Credit card
8. Zip code
Validating Required Fields
if the fields are blank or have null values, then error message should be displayed to fill up the fields.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating required fields</TITLE>
<SCRIPT type="text/javascript">
function validateReqFields(thisform)
{
var userName=thisform.uname.value;
var password=thisform.pw.value;
if(userName==null || userName=="")
{
alert("Enter the User name");
thisform.uname.focus();
return false;
}
if(password==null || password=="")
{
alert("Enter the password");
thisform.pw.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="Submit.html" onsubmit="return validateReqFields(this)" method="Post">
User Name:<input type= "text" name="uname"/>*<br/>
Password: <input type= "password" name="pw"/>*<br/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
Submit.html
</HTML>
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have filled all the required fields </H2>
</BODY>
</HTML>
Validating Numbers
you can use isNaN() function to validate a number object.
the isNaN() function returns false if the given value is a number and returns true if the given number is not a number.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Numbers</TITLE>
<SCRIPT type="text/javascript">
function validateNum(thisform)
{
var num = thisform.num.value;
if(num==null|| num=="")
{
alert("enter the number");
thisform.num.focus();
return false;
}
if(isNaN(num))
{
alert("it’s not a valid number");
thisform.num.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="NumberValidated.html" onsubmit="return validateNum(this)" method="Post">
Enter Number:<INPUT type="text" name="num"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
NumberValidated.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>you have entered a valid number </H2>
</BODY>
</HTML>
Validating User Name and Password
you ccan check the values of fields for null and blank as well as you can restrict the length of the values.
username field must have at least six characters,
password and confirmpassword must have at least eight characters,
and both passwords must be matched.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating User Name and Password</TITLE>
<SCRIPT type="text/javascript">
function validateUrPw(thisform)
{
var userName= thisform.uname.value;
var pass1= thisform.pw1.value;
var pass2= thisform.pw2.value;
if(userName=="")
{
alert("Please enter the user name");
thisform.uname.focus();
return false;
}
if(userName.length<6)
{
alert("User name must be of atleast six characters");
thisform.uname.focus();
return false;
}
if(pass1=="")
{
alert("Please enter the password");
thisform.pw1.focus();
return false;
}
if(pass1.length<8)
{
alert("Password must contain atleast 8 digits");
thisform.pw1.focus();
return false;
}
if(pass2=="")
{
alert("Please enter the confirm password");
thisform.pw2.focus();
return false;
}
if(pass2.length<8)
{
alert("Confirm password must contain atleast 8 digits");
thisform.pw2.focus();
return false;
}
if(pass1.length>=8 && pass2.length>=8 )
{
if(pass1!=pass2)
{
alert("Password mismatched! enter the correct password!");
return false;
}
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="UserDetails.html" onsubmit="return validateUrPw(this)" method="Post">
User Name:<INPUT type="text" name="uname"/><BR/>
Password:<INPUT type="password" name="pw1"/><BR/>
Confirm-Password:<INPUT type="password" name="pw2"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
UserDetails.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered the correct User Name and Password</H2>
</BODY>
</HTML>
Validating a Phone Number
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating phone number</TITLE>
<SCRIPT type="text/javascript">
function validatePhNum(thisform)
{
var ph=thisform.phnum.value;
if(ph==null||ph=="")
{
alert("enter the phone number");
thisform.phnum.focus();
return false;
}
if(ph.length<10)
{
alert("phone number must have atleast 10 digits");
thisform.phnum.focus();
return false;
}
if(isNaN(ph))
{
alert("Invalid phone number");
thisform.phnum.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidPhone.html" onsubmit="return validatePhNum(this)" method="Post">
Phone Number:<input type="text" name="phnum"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidPhone.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Phone number </H2>
</BODY>
</HTML>
Validating Time
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateTime(thisform)
{
var txtTime= thisform.time.value;
if(txtTime == "")
{
alert("Please enter the Time");
thisform.time.focus();
return false;
}
var pos=txtTime.indexOf(":");
if(pos<0)
{
alert("Invalid Time!colon(:) is missing between HH and MM");
thisform.time.focus();
return false;
}
if(pos>2 || pos<1)
{
alert("Invalid Time! Time format should be HH:MM AM/PM");
thisform.time.focus();
return false;
}
var hours= txtTime.substring(0,pos);
if(hours>12)
{
alert("Invalid Time! Hours cannot be greater than 12");
thisform.time.focus();
return false;
}
if(hours<=0)
{
alert("Invalid Time! Hours cannot be smallar than 0");
thisform.time.focus();
return false;
}
var min=txtTime.substring(pos+1, pos+3);
if(min>59)
{
alert("Invalid Time! Mins cannot be greater than 59");
thisform.time.focus();
return false;
}
if(min<0)
{
alert("Invalid Time! Mins cannot be smallar than 0");
thisform.time.focus();
return false;
}
var t1 = txtTime.substring(5,8);
if(t1!= "AM" && t1!= "am" && t1!="PM" && t1!= "pm")
{
alert("Invalid Time! time should be end with AM or PM");
thisform.time.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidTime.html" onsubmit="return validateTime(this)" method="Post">
Enter Time(HH:MM AM/PM):<input type="text" name="time"/><br/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
This method extracts the characters in a string between "start" and "end", not including "end" itself.
Validating Date
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Date</TITLE>
<SCRIPT type="text/javascript">
function isDate(txtDate)
{
var objDate;
var mSeconds;
if (txtDate=="")
{
alert("Please enter the Date");
return false;
}
var day = txtDate.substring(3,5);
var month = txtDate.substring(0,2)-1;
var year = txtDate.substring(6,10);
if (txtDate.substring(2,3) != '/')
{
alert("Please enter the Date in correct format");
return false;
}
if (txtDate.substring(5,6) != '/')
{
alert("Please enter the Date in correct format");
return false;
}
mSeconds = (new Date(year, month, day)).getTime();
objDate = new Date();
objDate.setTime(mSeconds);
if (objDate.getDate()!= day)
{
alert("you have entered the wrong Day");
return false;
}
if (objDate.getMonth()!= month)
{
alert("you have entered the wrong month");
return false;
}
if (objDate.getFullYear()!= year)
{
alert("you have entered the wrong year");
return false;
}
return true;
}
function checkDate(thisform)
{
var txtDate = thisform.date.value;
if (isDate(txtDate))
return true;
else
{
return false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidDate.html" onsubmit="return checkDate(this)" method="Post">
Enter Date(mm/dd/yyyy):<INPUT type="text" name="date"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidDate.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Date </H2>
</BODY>
</HTML>
Validating Credit Cards
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateCC(thisform)
{
var cardNum = thisform.CNum.value;
var cardType= thisform.CType.value;
if (cardType=="sc")
{
alert ("Please select the card");
thisform.CType.focus();
return false;
}
if (cardNum=="")
{
alert ("Enter your credit card number");
thisform.CNum.focus();
return false;
}
if (isNaN(cardNum))
{
alert ("it’s not an integer");
thisform.CNum.focus();
return false;
}
switch (cardType)
{
case"Master Card": var c = cardNum.substring(0,2);
if (cardNum.length!=16 ||c<51|| c>55)
{
alert ("Invalid Master card Number");
thisform.CNum.focus();
return false;
}
break;
case"Visa": var c = cardNum.substring (0,1);
if (cardNum.length!=16 && cardNum.length!=13 ||c!=4)
{
alert ("Invalid visa Number");
thisform.CNum.focus();
return false;
}
break;
case"Amex": var c = cardNum.substring(0,2);
if (cardNum.length!=15 || c<34 ||c>37)
{
alert ("Invalid Amex Number");
thisform.CNum.focus();
return false;
}
break;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidCard.html" onsubmit="return validateCC(this)" method="Post">
Card Type :<SELECT name= "CType">
<OPTION value="sc" selected> Select Card
<OPTION value="Master Card">Master Card
<OPTION value="Visa">Visa
<OPTION value="Amex">American Express
</SELECT><BR/>
Card Number :<INPUT type="text" name="CNum"><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidCard.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid card number </H2>
</BODY>
</HTML>
Validating Zip Code
in india the zip code of cities is of length 6, and the first digit between 1 and 8.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateZipCode(thisform)
{
var zipCode = thisform.zCode.value;
var c= zipCode.substring(0,1);
if (zpCode=="")
{
alert ("Enter the six digit zip code number");
thisform.zCode.focus();
return false;
}
if (isNaN(zipCode))
{
alert ("zip code must be an integer value");
thisform.zCode.focus();
return false;
}
if (zipCode.length!=6)
{
alert ("zip code must be of six digits");
thisform.zCode.focus();
return false;
}
if(c==0 || c==9)
{
alert ("not a valid zip code");
thisform.zCode.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidZip.html" onsubmit="return validateZipCode(this)" method="Post">
Enter Zip Code :<INPUT type="text" name="zCode"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidZip.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Zip Code </H2>
</BODY>
</HTML>
Using Regular Expressions to Validate Email Address
you can use regular expressions to
search for pattern in a string,
replace text,
extract sustring
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating E-mail</TITLE>
<SCRIPT type="text/javascript">
function validateEmail(thisform)
{
var txtEmail=thisform.email.value;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
var n1=txtEmail.indexOf("@");
var n2=txtEmail.lastIndexOf(".");
if(txtEmail=="")
{
alert("Enter the Email address");
thisform.email.focus();
return false;
}
if(txtEmail.match(illegalChars))
{
alert("Invalid Email address");
thisform.email.focus();
return false;
}
if(n1<1 ||n2-n1<2)
{
alert("Invalid Email address");
thisform.email.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY >
<FORM action="ValidEmail.html" onsubmit="return validateEmail(this)" method="Post">
User Name:<INPUT type="text" name="uname"/><BR/>
E-mail:<INPUT type="text" name="email"/>*<BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidEmail.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered the valid Email address </H2>
</BODY>
</HTML>
Handling Exceptions
an exception is an error that occurs at the time of execution due to an illegal operation when the program is syntactically correct.
1. using the try-catch statement
2. using the onerror event
using the try-catch statement
th try block contains the suspected code that can generate errors
if th error occurs, the try block throws an exception that is caught by the catch block.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using try-catch statement</TITLE>
<SCRIPT type="text/javascript">
try
{
document.write(n);
document.write("Hello World");
}
catch(err)
{
document.write(err.message);
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
the catch block must appear immediatly after the try block
there is another optional block kknown as finally block that is associatd with the try-catch statement.
the finally block of the try-catch statement always runs it's code whether or not an exception is thrown.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using finally block</TITLE>
<SCRIPT type="text/javascript">
try
{
document.write(n);
}
catch(err)
{
document.write(err.message);
}
finally
{
document.write("<BR/>");
document.write("Hello World");
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Using the onerror evnt
it displays the error message, the URL of the webpage, and th line number at which the error has occured.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using onerror event</TITLE>
<SCRIPT type="text/javascript">
window.onerror = function(ermessage, url, line)
{
document.write(ermessage+"<BR/>");
document.write(url+"<BR/>");
document.write(line+"<BR/>");
}
document.write(n);
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Validating Forms
1.Required fields
2.Numbers
3.User name and password
4. Phone number
5. Time
6. Date
7. Credit card
8. Zip code
Validating Required Fields
if the fields are blank or have null values, then error message should be displayed to fill up the fields.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating required fields</TITLE>
<SCRIPT type="text/javascript">
function validateReqFields(thisform)
{
var userName=thisform.uname.value;
var password=thisform.pw.value;
if(userName==null || userName=="")
{
alert("Enter the User name");
thisform.uname.focus();
return false;
}
if(password==null || password=="")
{
alert("Enter the password");
thisform.pw.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="Submit.html" onsubmit="return validateReqFields(this)" method="Post">
User Name:<input type= "text" name="uname"/>*<br/>
Password: <input type= "password" name="pw"/>*<br/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
Submit.html
</HTML>
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have filled all the required fields </H2>
</BODY>
</HTML>
Validating Numbers
you can use isNaN() function to validate a number object.
the isNaN() function returns false if the given value is a number and returns true if the given number is not a number.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Numbers</TITLE>
<SCRIPT type="text/javascript">
function validateNum(thisform)
{
var num = thisform.num.value;
if(num==null|| num=="")
{
alert("enter the number");
thisform.num.focus();
return false;
}
if(isNaN(num))
{
alert("it’s not a valid number");
thisform.num.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="NumberValidated.html" onsubmit="return validateNum(this)" method="Post">
Enter Number:<INPUT type="text" name="num"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
NumberValidated.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>you have entered a valid number </H2>
</BODY>
</HTML>
Validating User Name and Password
you ccan check the values of fields for null and blank as well as you can restrict the length of the values.
username field must have at least six characters,
password and confirmpassword must have at least eight characters,
and both passwords must be matched.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating User Name and Password</TITLE>
<SCRIPT type="text/javascript">
function validateUrPw(thisform)
{
var userName= thisform.uname.value;
var pass1= thisform.pw1.value;
var pass2= thisform.pw2.value;
if(userName=="")
{
alert("Please enter the user name");
thisform.uname.focus();
return false;
}
if(userName.length<6)
{
alert("User name must be of atleast six characters");
thisform.uname.focus();
return false;
}
if(pass1=="")
{
alert("Please enter the password");
thisform.pw1.focus();
return false;
}
if(pass1.length<8)
{
alert("Password must contain atleast 8 digits");
thisform.pw1.focus();
return false;
}
if(pass2=="")
{
alert("Please enter the confirm password");
thisform.pw2.focus();
return false;
}
if(pass2.length<8)
{
alert("Confirm password must contain atleast 8 digits");
thisform.pw2.focus();
return false;
}
if(pass1.length>=8 && pass2.length>=8 )
{
if(pass1!=pass2)
{
alert("Password mismatched! enter the correct password!");
return false;
}
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="UserDetails.html" onsubmit="return validateUrPw(this)" method="Post">
User Name:<INPUT type="text" name="uname"/><BR/>
Password:<INPUT type="password" name="pw1"/><BR/>
Confirm-Password:<INPUT type="password" name="pw2"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
UserDetails.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered the correct User Name and Password</H2>
</BODY>
</HTML>
Validating a Phone Number
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating phone number</TITLE>
<SCRIPT type="text/javascript">
function validatePhNum(thisform)
{
var ph=thisform.phnum.value;
if(ph==null||ph=="")
{
alert("enter the phone number");
thisform.phnum.focus();
return false;
}
if(ph.length<10)
{
alert("phone number must have atleast 10 digits");
thisform.phnum.focus();
return false;
}
if(isNaN(ph))
{
alert("Invalid phone number");
thisform.phnum.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidPhone.html" onsubmit="return validatePhNum(this)" method="Post">
Phone Number:<input type="text" name="phnum"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidPhone.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Phone number </H2>
</BODY>
</HTML>
Validating Time
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateTime(thisform)
{
var txtTime= thisform.time.value;
if(txtTime == "")
{
alert("Please enter the Time");
thisform.time.focus();
return false;
}
var pos=txtTime.indexOf(":");
if(pos<0)
{
alert("Invalid Time!colon(:) is missing between HH and MM");
thisform.time.focus();
return false;
}
if(pos>2 || pos<1)
{
alert("Invalid Time! Time format should be HH:MM AM/PM");
thisform.time.focus();
return false;
}
var hours= txtTime.substring(0,pos);
if(hours>12)
{
alert("Invalid Time! Hours cannot be greater than 12");
thisform.time.focus();
return false;
}
if(hours<=0)
{
alert("Invalid Time! Hours cannot be smallar than 0");
thisform.time.focus();
return false;
}
var min=txtTime.substring(pos+1, pos+3);
if(min>59)
{
alert("Invalid Time! Mins cannot be greater than 59");
thisform.time.focus();
return false;
}
if(min<0)
{
alert("Invalid Time! Mins cannot be smallar than 0");
thisform.time.focus();
return false;
}
var t1 = txtTime.substring(5,8);
if(t1!= "AM" && t1!= "am" && t1!="PM" && t1!= "pm")
{
alert("Invalid Time! time should be end with AM or PM");
thisform.time.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidTime.html" onsubmit="return validateTime(this)" method="Post">
Enter Time(HH:MM AM/PM):<input type="text" name="time"/><br/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
This method extracts the characters in a string between "start" and "end", not including "end" itself.
Validating Date
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Date</TITLE>
<SCRIPT type="text/javascript">
function isDate(txtDate)
{
var objDate;
var mSeconds;
if (txtDate=="")
{
alert("Please enter the Date");
return false;
}
var day = txtDate.substring(3,5);
var month = txtDate.substring(0,2)-1;
var year = txtDate.substring(6,10);
if (txtDate.substring(2,3) != '/')
{
alert("Please enter the Date in correct format");
return false;
}
if (txtDate.substring(5,6) != '/')
{
alert("Please enter the Date in correct format");
return false;
}
mSeconds = (new Date(year, month, day)).getTime();
objDate = new Date();
objDate.setTime(mSeconds);
if (objDate.getDate()!= day)
{
alert("you have entered the wrong Day");
return false;
}
if (objDate.getMonth()!= month)
{
alert("you have entered the wrong month");
return false;
}
if (objDate.getFullYear()!= year)
{
alert("you have entered the wrong year");
return false;
}
return true;
}
function checkDate(thisform)
{
var txtDate = thisform.date.value;
if (isDate(txtDate))
return true;
else
{
return false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidDate.html" onsubmit="return checkDate(this)" method="Post">
Enter Date(mm/dd/yyyy):<INPUT type="text" name="date"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidDate.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Date </H2>
</BODY>
</HTML>
Validating Credit Cards
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateCC(thisform)
{
var cardNum = thisform.CNum.value;
var cardType= thisform.CType.value;
if (cardType=="sc")
{
alert ("Please select the card");
thisform.CType.focus();
return false;
}
if (cardNum=="")
{
alert ("Enter your credit card number");
thisform.CNum.focus();
return false;
}
if (isNaN(cardNum))
{
alert ("it’s not an integer");
thisform.CNum.focus();
return false;
}
switch (cardType)
{
case"Master Card": var c = cardNum.substring(0,2);
if (cardNum.length!=16 ||c<51|| c>55)
{
alert ("Invalid Master card Number");
thisform.CNum.focus();
return false;
}
break;
case"Visa": var c = cardNum.substring (0,1);
if (cardNum.length!=16 && cardNum.length!=13 ||c!=4)
{
alert ("Invalid visa Number");
thisform.CNum.focus();
return false;
}
break;
case"Amex": var c = cardNum.substring(0,2);
if (cardNum.length!=15 || c<34 ||c>37)
{
alert ("Invalid Amex Number");
thisform.CNum.focus();
return false;
}
break;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidCard.html" onsubmit="return validateCC(this)" method="Post">
Card Type :<SELECT name= "CType">
<OPTION value="sc" selected> Select Card
<OPTION value="Master Card">Master Card
<OPTION value="Visa">Visa
<OPTION value="Amex">American Express
</SELECT><BR/>
Card Number :<INPUT type="text" name="CNum"><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidCard.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid card number </H2>
</BODY>
</HTML>
Validating Zip Code
in india the zip code of cities is of length 6, and the first digit between 1 and 8.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating Time</TITLE>
<SCRIPT type="text/javascript">
function validateZipCode(thisform)
{
var zipCode = thisform.zCode.value;
var c= zipCode.substring(0,1);
if (zpCode=="")
{
alert ("Enter the six digit zip code number");
thisform.zCode.focus();
return false;
}
if (isNaN(zipCode))
{
alert ("zip code must be an integer value");
thisform.zCode.focus();
return false;
}
if (zipCode.length!=6)
{
alert ("zip code must be of six digits");
thisform.zCode.focus();
return false;
}
if(c==0 || c==9)
{
alert ("not a valid zip code");
thisform.zCode.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="ValidZip.html" onsubmit="return validateZipCode(this)" method="Post">
Enter Zip Code :<INPUT type="text" name="zCode"/><BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidZip.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered a valid Zip Code </H2>
</BODY>
</HTML>
Using Regular Expressions to Validate Email Address
you can use regular expressions to
search for pattern in a string,
replace text,
extract sustring
<!DOCTYPE HTML>
<HEAD>
<TITLE>Validating E-mail</TITLE>
<SCRIPT type="text/javascript">
function validateEmail(thisform)
{
var txtEmail=thisform.email.value;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
var n1=txtEmail.indexOf("@");
var n2=txtEmail.lastIndexOf(".");
if(txtEmail=="")
{
alert("Enter the Email address");
thisform.email.focus();
return false;
}
if(txtEmail.match(illegalChars))
{
alert("Invalid Email address");
thisform.email.focus();
return false;
}
if(n1<1 ||n2-n1<2)
{
alert("Invalid Email address");
thisform.email.focus();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY >
<FORM action="ValidEmail.html" onsubmit="return validateEmail(this)" method="Post">
User Name:<INPUT type="text" name="uname"/><BR/>
E-mail:<INPUT type="text" name="email"/>*<BR/>
<INPUT type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
ValidEmail.html
<!DOCTYPE HTML>
<HEAD>
<TITLE>submit form</TITLE>
</HEAD>
<BODY>
<H2>You have entered the valid Email address </H2>
</BODY>
</HTML>
Handling Exceptions
an exception is an error that occurs at the time of execution due to an illegal operation when the program is syntactically correct.
1. using the try-catch statement
2. using the onerror event
using the try-catch statement
th try block contains the suspected code that can generate errors
if th error occurs, the try block throws an exception that is caught by the catch block.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using try-catch statement</TITLE>
<SCRIPT type="text/javascript">
try
{
document.write(n);
document.write("Hello World");
}
catch(err)
{
document.write(err.message);
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
the catch block must appear immediatly after the try block
there is another optional block kknown as finally block that is associatd with the try-catch statement.
the finally block of the try-catch statement always runs it's code whether or not an exception is thrown.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using finally block</TITLE>
<SCRIPT type="text/javascript">
try
{
document.write(n);
}
catch(err)
{
document.write(err.message);
}
finally
{
document.write("<BR/>");
document.write("Hello World");
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Using the onerror evnt
it displays the error message, the URL of the webpage, and th line number at which the error has occured.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Using onerror event</TITLE>
<SCRIPT type="text/javascript">
window.onerror = function(ermessage, url, line)
{
document.write(ermessage+"<BR/>");
document.write(url+"<BR/>");
document.write(line+"<BR/>");
}
document.write(n);
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
No comments:
Post a Comment