开发者

Verification function for HTML form data

I've created a function in JavaScript to verify an html form data, my code as below:

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(开发者_如何学Go);
        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;
    }

}

The verifications are working good until "phone2" field and will not complete the verification after this.

I'll do appreciate if you can help me and advise how to solve this.


I think you are getting an exception as isNumeric is not a JavaScript Global function. You need to define it in your page (check out Validate decimal numbers in JavaScript - IsNumeric() for a clean implementation of isNumeric). Also you should surround your method call with exception handling to get better details of the exception.


In that line you're actually checking phone2 only in the first condition, the others are phone1.

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

Also be aware that you do the same for phone3.


It looks like a simple copy/paste error. Notice that the petition_form members referenced after phone2 are phone1... this does not make sense. Compare this line against your next validation where all of the members are phone1.

So, this line:

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

Should look like:

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

(Code is lined up in that manner to hilight the differences.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜