0

I have the following code:

HTML

<input name="FirstName" id="FirstName" class="cat_textbox" maxlength="255" type="text" placeholder="First Name*"> 

<input name="LastName" id="LastName" class="cat_textbox" maxlength="255" type="text" placeholder="Last Name*">

<input name="EmailAddress" id="EmailAddress" class="cat_textbox" maxlength="255" type="text" placeholder="E-Mail Address*">

JS

$( ".prep-form" ).on( "click", function() {

var name = $.trim($('#FirstName').val()),
    nachName = $.trim($('#LastName').val()),
    eMail = $.trim($('#EmailAddress').val());

if (name == '' || nachName == '' || eMail == '') {

    // SHOW ERROR MESSAGE

} else if //Continue with script -- Note: The script works fine until I add the above code...

  return false;
});

As you can see I am checking three input fields to see if they are empty. If any of the three are empty I show an error message, otherwise the script should continue to run.

When I try the above code, regardless of what is in the input fields, the script will only show the error message.

What is wrong?

11
  • 2
    If any of the three are empty I show an error message. Shouldn't there be an OR condition? Commented Jun 30, 2015 at 9:10
  • @PramodKarandikar - You are correct. I edited my code, however, the issue still arises. Commented Jun 30, 2015 at 9:12
  • @Tushar - The other code is irrelevant and the script works if I remove the if statement I posted. Commented Jun 30, 2015 at 9:13
  • You can try the other way if (name || nachName || eMail ) {// valid } else { throw error } Commented Jun 30, 2015 at 9:15
  • I would check against the length of the String-values. like: name.length === 0 || ... also: don't use 'trim' against a jQuery-Object (see Mukesh Kalgude's answer). Commented Jun 30, 2015 at 9:17

1 Answer 1

1

I am unsure what was wrong with my code but I found a solution using the following code:

var x = document.forms["formName"]["FirstName"].value;
var y = document.forms["formName"]["LastName"].value;
var z = document.forms["formName"]["EmailAddress"].value;

if (x == null || x == "") {

    //Show Alert

return false;

} else if (y == null || y == "") {

    //Show Alert

return false;

} else if (y == null || y == "") {

    //Show Alert

return false;

} else { 

//Once those above validate the script continues running
Sign up to request clarification or add additional context in comments.

Comments

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.