Jump to content

validate on else statement only and not if and else


toolman

Recommended Posts

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 want to remove the "Nice!..." part if the email address is ok and just have the form submit. How can I do this?

By removing that part. Literally. Not sure why you didn't consider that.

 

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?

e.preventDefault();
is what would have done the job if e was the event for the button click and not from the document.onready. So

$(document).ready(function() {
    $('#email-submit').click(function(e) {
And really, this code should be executing on the form submission, not on the button click.

$(document).ready(function() {
    $('some selector for your form').submit(function(e) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.