-1

Below is a simplified snippet of code from a form validation script. It does not work...

When the form is submitted and the user has left "inputfield" empty, the first alert pops up. After the user has entered something in "inputfield", the second alert pops up when the user clicks submit.

Afterwards, the script should return false and continue validation. I need both alerts to be included in the same else if. What am I doing wrong? The alerts do not show and the form validation ignores this portion of code...

    } else if (myform.inputfield.value=="") {
    alert ('Please enter something in the text field!');
    }
    if (myform.inputfield.value!=="") {
    alert ('Thank you for entering something in the field!');
    return false;
    }
4
  • How are you calling the code? Also, you should probably nowadays use document.getElementByIdto find the field. Commented Apr 9, 2014 at 6:16
  • Did you try replacing "" with NULL or removing return false? Commented Apr 9, 2014 at 6:19
  • first try to do a console.log(myform.inputfield.value) when the textbox contains a value and when it doesnt, i'm sure you will get a null value. Then like Peitu said, use document.getElementById. this is 2014 ;) Commented Apr 9, 2014 at 6:21
  • Calling the code with <form onsubmit="return validate(this)". Will work on all other sugestions, thank you! Commented Apr 9, 2014 at 6:29

2 Answers 2

1

Isn't it better to declare a flag and return the it as a result? take the following sample into account:

var validationResult = true;

...
} else if (myform.inputfield.value=="") {
   validationResult = false;
   alert ('Please enter something in the text field!');
}
if (myform.inputfield.value!=="") {
   alert ('Thank you for entering something in the field!');
   validationResult = false;
}

return validationResult;
Sign up to request clarification or add additional context in comments.

Comments

1

I agree rather use document.getElementById, or jQuery $('#input1').val()

 <script type="text/javascript">
    function validate() {
        if (document.getElementById('input1').value == "") {
            alert('Please enter something in the text field!');
        }
        if (document.getElementById('input2') !== "") {
            alert('Thank you for entering something in the field!');
            return false;
        }
    }
</script>
</head>
<body>
    <form onsubmit="return validate()">
        <input id="input1" />
        <input id="input2" />
        <input type="submit" value="OK" />
    </form>
</body>

You might have a return statement before the specified line of code?

7 Comments

I have a return false;
If you have return false; before the alert validation, the alert validation will not be reached.
It is reached, but ignores "Please enter something in the text field!" and jumps to "Thank you for entering something in the field!"... Argh!
okay, what does this line preduce when adding it to the top of you validation code alert(myform.inputfield.value)
also note that your "Please enter something in the text field!" is in a else if, if the first statement is satisfied the else if will be skipped.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.