PDA

View Full Version : Javascript RegExp in Forms



ExpDisease
26 Aug 2010, 07:33 AM
I cannot get the result right. I write area and phone by numbers but it still gives me error



function validate_text(field,alerttext) {
with(field) {

var re = /[a-zA-Z]/g;
if(re.test(field)) { return true;}else{alert(alerttext);return false;}
}
}
function validate_number(field,alerttext){
with(field) {

var re = /[0-9]/g;
if(re.test(field)) { return true;}else{alert(alerttext);return false;}
}
}



function validateForm(thisForm){
with(thisForm)
{
if(validate_number(area,"wrong area code") == false) {

area.focus();return false;
}
if(validate_number(phone,"wrong phone") == false) {

phone.focus();return false;
}
if(validate_text(name_surname,"wrong name") == false) {

name_surname.focus();return false;
}

}
}


...<b>Name Surname :</b><input style="margin-left:10px;" type="text" name="name_surname"/>
<br /><br />
<b>phone :</b><input type="text" style="margin-left:19px;" maxlength="4" size="4" name="area" />
-<input type="text" style="margin-left:5px;" maxlength="7" size="10" name="phone" /><br /><br />....

gandalf117
31 Aug 2010, 06:37 AM
It still gives you an error, because you are submitting the input objects to the validation functions not the input values.

validate_number(area,"wrong area code") should be: validate_number(area.value,"wrong area code")
validate_number(phone,"wrong phone") should be: validate_number(phone.value,"wrong phone")
validate_text(name_surname,"wrong name") should be: validate_text(name_surname.value,"wrong name")


This won't solve your problem completely though. I can't say why but after validating for every character in a string, it will fail after that even with the correct input. Thus if you have a four digit area code it will validate correctly the first four times and fail on the fifth time you click the validate button. I am not sure why but try it and see. I am sure it has to do with the function re.test() and the pattern. Therefore I used function isNaN() instead, which checks whether a string doesn't contain any numbers (returns true) or if it does (returns false). Here is the final code:



<head>
<script type='text/javascript'>
function validate_text(field,alerttext) {
with(field) {
//var re = /[a-zA-Z]/g;
//if(re.test(field))
if(isNaN(field)) { return true;}else{alert(alerttext);return false;}
}
}
function validate_number(field,alerttext){
with(field) {
//var re = /[0-9]/g;
//if(re.test(field))
if(!isNaN(field)) { return true;} else{alert(alerttext);return false;}
}
}




function validateForm(thisForm){
with(thisForm)
{
if(validate_number(area.value,"wrong area code") == false) {

area.focus();
return false;
}
if(validate_number(phone.value,"wrong phone") == false) {

phone.focus();
return false;
}
if(validate_text(name_surname.value,"wrong name") == false) {

name_surname.focus();
return false;
}
}
}
</script>
</head>

<body>
<form action="" method="post">
<b>Name Surname :</b>
<input style="margin-left:10px;" type="text" name="name_surname" />
<br /><br />
<b>phone:</b>
<input type="text" style="margin-left:5px;" maxlength="7" size="10" name="phone" />
<b>area:</b>
<input type="text" style="margin-left:19px;" maxlength="4" size="4" name="area" />
<br /><br />
<input type="button" value="validate" onclick="javascript:validateForm(this.form);" />
<br /><br />
</form>
</body>