我在JavaScript中创建了一个函数来验证html表单数据,我的代码如下:
function checkPetitionForm_ff() {
if (document.petition_form.petition_firstname.value == "FIRST NAME" || document.petition_form.petition_firstname.value == "") {
alert("Please enter your First Name!")
document.petition_form.petition_firstname.focus();
return false;
}
if (document.petition_form.petition_lastname.value == "LAST NAME" || document.petition_form.petition_lastname.value == "") {
alert("Please enter your Last Name!")
document.petition_form.petition_lastname.focus();
return false;
}
if (document.petition_form.petition_age.value == "AGE" || document.petition_form.petition_age.value == "") {
alert("Please enter your Age!")
document.petition_form.petition_age.focus();
return false;
}
if (document.petition_form.state.value == "Select State") {
alert("Please select your state!")
document.petition_form.state.focus();
return false;
}
if (document.petition_form.petition_address.value == "HOME ADDRESS" || document.petition_form.petition_address.value == "") {
alert("Please enter your address!")
document.petition_form.petition_address.focus();
return false;
}
if (document.petition_form.zip.value == "ZIP CODE" || document.petition_form.zip.value == "") {
alert("Please enter your Zipcode!")
document.petition_form.zip.focus();
return false;
}
if (document.petition_form.phone2.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
alert("Please enter the complete phone No!")
document.petition_form.phone2.focus();
return false;
}
if (document.petition_form.phone1.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
alert("Please enter the complete phone No!")
document.petition_form.phone1.focus();
return false;
}
if (document.petition_form.phone3.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
alert("Please enter the complete phone No!")
document.petition_form.phone3.focus();
return false;
}
if (document.petition_form.level.value == "YOUR LEVEL OF EDUCATION") {
alert("Please select your level of education!")
document.petition_form.level.focus();
return false;
}
if (document.petition_form.degree.value == "DEGREE OF INTEREST") {
alert("Please select your degree!")
document.petition_form.degree.focus();
return false;
}
if (!(document.getElementById(edu).checked)) {
alert("Please select Education!")
document.petition_form.edu.focus();
return false;
}
else {
return true;
}
}
验证工作正常,直到“phone2”字段,并且在此之后将无法完成验证。
如果你能帮助我并建议如何解决这个问题,我将不胜感激。
答案 0 :(得分:1)
它看起来像一个简单的复制/粘贴错误。请注意petition_form
之后引用的phone2
成员是phone1
...这没有意义。将此行与您的下一个验证进行比较,其中所有成员都是phone1。
所以,这一行:
if (document.petition_form.phone2.value == "PHONE" ||
document.petition_form.phone1.value == "" ||
isNumeric(document.petition_form.phone1.value) == false) {
应该是这样的:
if (document.petition_form.phone2.value == "PHONE" ||
document.petition_form.phone2.value == "" ||
isNumeric(document.petition_form.phone2.value) == false) {
(代码以这种方式排列,以区分差异。)
答案 1 :(得分:1)
在该行中,您实际上只在第一个条件中检查phone2
,其他的是phone1
。
document.petition_form.phone2.value=="PHONE" || document.petition_form.phone1.value=="" || isNumeric(document.petition_form.phone1.value)==false
另请注意,您对phone3
执行了同样的操作。
答案 2 :(得分:1)
我认为您正在获得异常,因为isNumeric
不是JavaScript全局函数。您需要在页面中定义它(请查看Validate decimal numbers in JavaScript - IsNumeric()以获得isNumeric
的干净实现。
此外,您应该使用异常处理来包围方法调用,以获得更好的异常详细信息。