2011-09-29 63 views
-3

我創建了一個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

如果您定義了驗證標準(例如電話號碼必須包含空格,數字和「+」),然後說出問題所在,那麼人們更可能會幫助您。 – jornb87

+0

該函數僅驗證前7個字段而已,我不知道爲什麼,我更改了手機字段並添加了正確的名稱,但仍然有相同的問題 – Pipo

+0

謝謝大家,我已經解決了這個問題,我試過了發佈正確的代碼,但由於網站規則而失敗。 – Pipo

回答

1

我想你會得到一個異常,因爲isNumeric不是JavaScript全局函數。您需要在頁面中定義它(查看Validate decimal numbers in JavaScript - IsNumeric()以獲得乾淨的實施isNumeric)。 你也應該圍繞你的方法調用與異常處理,以獲得更好的例外細節。

+0

感謝添加劑 – Pipo

1

它看起來像一個簡單的複製/粘貼錯誤。請注意,在phone2之後引用的petition_form成員是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) { 

+0

只是我用phone1取代phone2來檢查是否會工作,當我讓phone1領域的第一個功能驗證它,當我用phone2取代它的作品太 – Pipo

1

在這行你」實際上只在第一種情況下檢查phone2,其他人是phone1

document.petition_form.phone2.value=="PHONE" || document.petition_form.phone1.value=="" || isNumeric(document.petition_form.phone1.value)==false 

另請注意,您對phone3也是這樣做的。

+0

我做了這個,但仍然有同樣的問題 – Pipo