我創建了一個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」欄,將無法完成此後驗證。
如果你能幫助我,並建議如何解決這個問題,我會很感激。
如果您定義了驗證標準(例如電話號碼必須包含空格,數字和「+」),然後說出問題所在,那麼人們更可能會幫助您。 – jornb87
該函數僅驗證前7個字段而已,我不知道爲什麼,我更改了手機字段並添加了正確的名稱,但仍然有相同的問題 – Pipo
謝謝大家,我已經解決了這個問題,我試過了發佈正確的代碼,但由於網站規則而失敗。 – Pipo