【发布时间】:2010-03-07 01:43:35
【问题描述】:
好的,我有一个验证脚本,可以检查表单上的所有内容 - 但无论其中有什么,它都会将电话号码字段标记为错误。我已经尝试了几种不同的方法,但我无法弄清楚我做错了什么。
验证的脚本部分是...
if (testPattern(phone1, /^\d{3}$/)== false) { // checking phone length
valid = false;
}
if (testPattern(phone2, /^\d{3}$/)== false) {
valid = false;
}
if (testPattern(phone3, /^\d{4}$/)== false) {
valid = false;
}
功能码是……
function testPattern(field, reg2) {
var trueOrfalse = reg2.test(field)
if (trueOrfalse == false) {
field.style.backgroundColor="yellow"; // if false, change colors and return false
field.style.color="red";
return false;
}
else {
field.style.backgroundColor="white"; // if true, change colors and return true
field.style.color="black";
return true;
}
}
【问题讨论】:
标签: javascript validation forms