这是我的代码,我不知道它在哪里坏了。它的运行就像它从不检查变量中写入的正确正则表达式一样。任何帮助找出我哪里出错了,我们将不胜感激。
<script>
function allNumbers( field, msg ) {
var numberexp = /^[0-9]+$/;
if ( field.value.match( numberexp ) ) {
return true;
} else {
alert( msg );
field.focus();
return false;
}
}
function allLetters( field, msg ) {
var letexp = /^[a-zA-Z]+$/;
if ( field.value.match( letexp ) ) {
return true;
} else {
alert( Msg );
field.focus();
return false;
}
}
function notEmpty( field, msg ) {
if ( field.value.length == 0 ) {
alert( msg );
field.focus();
return false;
}
return true;
}
function validateForm() {
var a = document.getElementById('firstname');
var b = document.forms["contactrecord"]["lastname"].value;
var c = document.forms["contactrecord"]["phone"].value;
var d = document.forms["contactrecord"]["address"].value;
var e = document.forms["contactrecord"]["city"].value;
var f = document.forms["contactrecord"]["state"].value;
var g = document.forms["contactrecord"]["zip"].value;
if ( allLetters( a, "Incorrect First Name" ) ) {
if ( allLetters( b, "Incorrect Last Name" ) ) {
if ( allNumbers( c, "Incorrect Phone Number" ) ) {
if ( notEmpty( d, "Incorrect address" ) ) {
if ( allLetters( e, "Incorrect City Name" ) ) {
if ( allNumbers( g, "Incorrect Zip Code") ) {
return true;
}
}
}
}
}
}
return false;
}
</script>