How can i have just this else statement instead of if else

Hi,

I have the following code which checks email addresses are valid. I want to remove the “Nice!..” part if the email address is ok and just have the form submit. How can I do this?

$(document).ready(function(e) {
    $('#email-submit').click(function() {
        var sEmail = $('#email').val();
        // checking empty fields
        
        if (validateEmail(sEmail)) {
            alert('Nice!! your Email is valid, now you can continue..');
        }
        else {
            alert('Invalid Email Address');
            e.preventDefault();
        }
    });
});

Also, I have noticed if the email address is invalid, the alert works, but then goes to a page with the form on - like an external page. The form action is linking to an external URL as the action is hosted externally. Is there a way I can stop the form submitting when the email is invalid?

Hope than makes sense.

Thanks!

I think you can just return true; it? You should have it be on the form submit though, not on click

As @RyanReese indicated, you could just return true for the if portion.

Or, if you want just an IF with no ELSE, then change the boolean of the IF and put the ELSE code within.

if(!validateEmail(sEmail)){  // Exclamation mark == 'not'
  alert('Invalid Email Address');
  e.preventDefault();
  return false; // in case preventDefault() doesn't work.
  } 

HTH,

:slight_smile:

Thanks, this worked :smiley:
However, when it prompts that an invalid email is entered, the page then loads a new page with the form on again - on an external URL. Is this because the form action is on an external URL? If so, is it possible to stop it from going any further until a valid email is added?

Thanks!

No, it’s most likely because the event handler is calling the function without “return” pre-pended to it. If you use something like (pseudo-code) onSubmit = "return functionName();" then the return false should prevent the form submit.

HTH,

:slight_smile:

UPDATE: Okay… looking closer at your code, you’re not using onSubmit or something like it. I’m assuming that “email-submit” is a submit button? Try changing that to an input type="button" so the submit button won’t submit, and use formname.submit() (whatever the form name/id is) to submit.

That’s because you are triggering on a ‘click’ of the submit button rather than on a ‘submit’ of the form.

Thanks for the replies. I am a little confused now.

This is what I now have:

$(document).ready(function(e) {
    $('#email-submit').click(function() {
        var sEmail = $('#email').val();
        // checking empty fields
        
        if(!validateEmail(sEmail)){  // Exclamation mark == 'not'
  alert('Invalid Email Address');
  e.preventDefault();
  return false; // in case preventDefault() doesn't work.
  }
    });
});

// Function that validates email address through a regular expression.
function validateEmail(sEmail) {
      var filter =  /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    
   if (filter.test(sEmail)) {
        return true;
    }
    else {
        return false;
    }
}

and my form HTML is:

<form action="http://website.com/form.php" method="post">
<input type="hidden" name="rcode" value="news">
<input type="hidden" name="aid" value="2096821975">
<input type="hidden" name="n" value="1">
<input type="hidden" name="a" value="1">
<input type="hidden" name="fsub" value="2012378">
Email Address:
<input type="text" name="email" id="email" style="width:190px">
<input type="submit" name="submit" value="submit" id="email-submit">
</form>

How do I add the formname.submit() to the process?

Thanks!

It would go AFTER the if conditional validating email. And you would replace formname with the actual name/id of your form.

Also, replace your submit button with an input type="button" so the form doesn’t submit if it doesn’t pass validation (keep the button name, of course).

:slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.