1

I am trying to validate an email address for a form element. I am familiar with regular expressions so I believe that part is correct, but I want it to display an alert when the email entered is invalid. My problem is that the form submits even when I enter an invalid email address instead of popping up the alert window.

RegEx function in javascript:

function validateEmail()
            {
                var myEmailRegEx = /\w+@\w+\.[a-z]|[A-Z]|\d|\.|-{2,}/

                if(myEmailRegEx.test(document.getElementById("EmailAddress")))
                {
                    return true;
                }
                else
                {
                    alert("That is not a valid email address");
                    return false;
                }
            }

form HTML:

<input type="text" name="Email" id="EmailAddress" size="50" />
        <br />
        <input type="submit" value="Submit Email" onClick="validateEmail();" />

4 Answers 4

1

Just for the sake of clarity, the problem with what you were doing was that you were giving an object to the regex for testing, which always returns true(weird though). With the .value we get the string that the field holds and then test the regex against it.

you should use,

var myEmailRegEx = new RegExp("/\w+@\w+\.[a-z]|[A-Z]|\d|\.|-{2,}")
myEmailRegEx.test(document.getElementById("EmailAddress").value)

Suppose that email address is present in a form with id = "my_form". To prevent the default submission of that form, you would use

$("#my_form").preventDefault();

if the email turns out to be true, you could then submit the form.

if(myEmailRegEx.test(document.getElementById("EmailAddress").value)){
   document.getElementById("mu_form").submit();
}

that should be it. I hope you have an idea where to plug in these code snippets :-)

Sign up to request clarification or add additional context in comments.

3 Comments

ok. now the if statement works. thanks! but when the user clicks "ok" on the alert window, the form submits anyway. is there a way to cancel the submission if the validation fails?
you should use the prevent default function to prevent submission. I will just make an edit for that. BTW, could you accept my answer?
i will. it says i have to wait 5 minutes before i can accept the answer. as soon it as it allows me to I will though.
1

Could just do:

<input type="email" />

and be done with it.

Comments

0

Try using an onchange event on the input field contain the email. For example:

<input type="text" name="Email" id="EmailAddress" size="50" onchange="validateEmail();" />
<br />
<input type="submit" value="Submit Email"/>

The above approach will call your validateEmail function whenever the value changes in that field.

Comments

0

While this doesn’t directly answer your question, it should be noted that your regex returns a lot of false negatives. For example, and email address with a . in the address, such as my personal email address, [email protected]

A good starting place is to start here

I will copy the "basic" email regex below for convenience.

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.