我正在做表单验证并且我的代码有问题:
var reValue = /^\s*$/;
var phoneClass = /(^|\s)phoneValidate(\s|$)/;
var phoneValue =
/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
for (var i=0; i < iLen; i++) {
el = allElements[i];
if(phoneClass.test(el.className) && reValue.test(el.value)){
// The field is empty.
aMessages += 'Please enter a phone number in: '+ el.name +'.\n';
}
else if (phoneClass.test(el.className) && phoneValue.test(el.value)) {
// The field is not a valid phone number
// Advise user to fix it
aMessages += 'Please enter a valid phone number in: '+ el.name +'.\n';
}
}
如果该字段为空,则第一个 IF 语句有效并显示消息,但 ELSE IF 由于某种原因不...我做错了什么?