-3

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();
        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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pipo
  • 299
  • 5
  • 24
  • If you define you verification criteria (e.g. phone number must consist of spaces, numbers and `+`) and then say where you are having a problem, people are more likely to help you. – jornb87 Sep 29 '11 at 20:39
  • the function is only validate the first 7 fields only, I don't know why, I've changed phones fields and added the correct names, but still have the same problem – Pipo Sep 29 '11 at 21:19
  • Thanks all, I've solved the problem, I tried to publish the correct code but failed due to the website rules. – Pipo Sep 29 '11 at 22:10

3 Answers3

1

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.)

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • just i replaced phone2 with phone1 to check if will work, when i make phone1 field first the function validate it, and when i replaced with phone2 it works too – Pipo Sep 29 '11 at 20:41
1

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.

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

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.

Community
  • 1
  • 1
user961954
  • 3,214
  • 2
  • 17
  • 24