Jump to content

how to validate email in form


jasonc

Recommended Posts

I just can not seem to find a javascript code that checks if the email format is correct. I know that someone could enter an bogus email address but thats ok. I am just after checking if the format of the email entered looks correct before the form is submitted.
Please can I have some suggestions of what others have used.

It could be that I am doing this wrong...

if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email.value.lrtrim())) {
errorMessage = 'Email contains invalid character/s';

Link to comment
Share on other sites

    const emailIsValid = (email) => {
        return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
    };

    email.addEventListener('change', () => {
        let status = emailIsValid(email.value);
        console.log('Email Address', email.value, 'Status', status);
        if (!status) {
            email.value = "";
            email.placeholder = "Email Address is Invalid!";
            email.style.borderColor = "red";
            email.focus();
        } else {
            email.style.borderColor = myBorder;
            sendEmail.email = email.value;
            sendStatus.email = true;
        }
    });

It's still best to check it server side and HTML5 make it required.

Edited by Strider64
Link to comment
Share on other sites

Thank you for this, but how do I add this in to my existing code like above ?

I already have the errormessage added to the top of the field if it is needed.

I already have it checked server side but still could fail when we reply to their message anyway.

Link to comment
Share on other sites

11 minutes ago, jasonc said:

Thank you for this, but how do I add this in to my existing code like above ?

I already have the errormessage added to the top of the field if it is needed.

I already have it checked server side but still could fail when we reply to their message anyway.

Well, I would if the errormessage is the trigger then just wipe out the input value (email.value = "";) for the email, have it required and show the error message. HTML5 won't allow a user to proceed.

 

Maybe?

if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value)) {
errorMessage = 'Email contains invalid character/s'; 
email.value = "";
}

Though it really isn't checking totally for invalid characters just the proper format. (That's what I'm concerned about)  I think most people are pretty careful in typing their email address.

Edited by Strider64
Link to comment
Share on other sites

4 minutes ago, jasonc said:

I did use the HTML5 but it is not supported on most andriod devices

Hmm? I didn't not know that as I'm a iPhone user. I find it strange that android devices would do that. I think a work around would to check the email.value to see if it's empty?

Edited by Strider64
Link to comment
Share on other sites

I would like to cater for all devices/pc.  I just tried the if statement you gave and it allowed

ssss@gg@.gfh

Just checking that the email is looking ok is fine, i.e. no spaces, no strange characters and that. They could still type in thisemaildoesnotexists@gmailfake.com  but then we'd see that when we go to reply !

Link to comment
Share on other sites

8 hours ago, jasonc said:

I did use the HTML5 but it is not supported on most andriod devices

I don't know exactly what you mean by this, but android supports HTML 5 semantic markup.

 

If you want to make sure the email actually exists, I believe there are services that can do this. I've not used one so I can't vouch for the accuracy or operation, but a Google search should return some good places to start.

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.