...

View Full Version : validating form



d1lu5ion
12-16-2010, 12:11 AM
I managed to create the form that asks you to type in the information but I'm having some difficulty trying to figure out the alert to say "Thank you " after you have everything filled in and then hitting the button.:(

my code is




<HTML>
<HEAD>
<TITLE> Form Validation Example </TITLE>
<SCRIPT LANGUAGE="JavaScript">

function validatePersonalInfo(){
var _first = document.info.fname.value;
var _city = document.info.city.value;
var _phone = document.info.phone.value;

if(_first.toString() == ""){alert("Please enter a first name.");}
if(_city.toString() == ""){alert("Please enter your city.");}
if(_phone.toString() == ""){alert("Please enter your phone number.");}

var phoneInput = document.info.phone.value;
var validPhone = false;
var validCity = false;
if(checkCity == true){
validCity = true;
}
else{

if(!checkPhone(phoneInput)){
alert("Phone number is invalid." + validPhone);
}
else{
validPhone = true;
}
if(validCity && validPhone){
alert("Your form has been verified");
}
}
}

function checkPhone(str){
var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/;
return regexp.test(str);
}
function checkNum(length){
var cityLet = parseInt(cityEntry, 10);
if (document.info.city.value.length == length){
if(cityLet != 0 && isNaN(cityLet) == false){
return true;
}
else {
return false;
}
}
else {
return false;
}
}

</script>
</head>

<body>
<p>
<form name="info" action="" method="post">
<table>
<tr><td align="left">First Name:</td>
<td align="left">
<input type="text" name="fname" size=15>
</td>
</tr>
<br>
</tr>
<br>
<tr>
<td align="left">City:</td>
<td align="left">
<input type="text" name="city" size=15>
</td>
</tr>
<br>
<tr><td align="left">phone</td>
<td align="left">
<input type="text" name="phone" size=20></td>
</tr>
<br>
</tr>
<br>
</table>
<center>
<input type="button" value="Submit" onClick="validatePersonalInfo()">
</center>
</form>
</body>
</html>

the help is greatly appreciated :)

Logic Ali
12-16-2010, 07:20 AM
Standard reply #1: Use the error console.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum