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!

Link to comment
Share on other sites

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) {
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.