0

Possible Duplicate:
What is the best regular expression for validating email addresses?

Hi All,

I have this regex/code to validate a email address:

var testEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

if ( testEmail.test(oForm["email"]) == false){
    chErrorMessage += "\nValid email address";
    bSubmit = false;
}

The code doesn't seem to be doing the test correctly, whether the email is formatted correctly or not.

I'm a beginner at JavaScript. Any help would be greatly appreciated, Thanks

Community
  • 1
  • 1
Nasir
  • 4,785
  • 10
  • 35
  • 39
  • 1
    Can you explain the result? Is your chErrorMessage getting the "Valid email address" message when the email is not valid? Or the opposite? Or does it just do one thing every time? – rdevitt Apr 28 '11 at 16:07
  • 3
    Note that `日本語@gmail.com` and `foo@例子.测试` are a valid email addresses. Regex-matching the email address on latin characters is soo 1990. Further there are also tlds longer than 4 chars like `.museum`. – BalusC Apr 28 '11 at 16:10
  • Can you show more code? The regex test seems to be working fine: [test](http://jsfiddle.net/kUWdd/1/). – Geert Apr 28 '11 at 16:10
  • 1
    Please don't add ‘HELP’ et al. to your question. We know you want our help, otherwise you wouldn't ask a question here. Adding HELP will not get you faster answers. – Marcel Korpel Apr 28 '11 at 16:20
  • 2
    See also: http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – KatieK Apr 28 '11 at 16:25

3 Answers3

2

I use the jQuery Validation Plugin for this: http://docs.jquery.com/Plugins/validation

It supports email address validation.

Teddy
  • 18,357
  • 2
  • 30
  • 42
1

Firstly, I'd get the input object of the form this way

document.oForm["email"]

instead of

oForm["email"]

Then, you should get the value of the input, not the input itself:

document.oForm["email"].value

Also, you want to execute the conditional code if the result is true, not false, so the condition should be:

testEmail.test(document.oForm["email"].value) == true

But it is not very elegant to compare a boolean value to true or false, since the compared value itself is enough to be used as the condition:

if (testEmail.test(document.oForm["email"].value)) {
    // ...
}

Your code worked for me after these changes. However, I would use the solution with JQuery because it would take care of all the infinite details your regex ignores about e-mails. OTOH, I hope you got a better understanding of how regexes work in JavaScript anyway.

HTH

brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • Thanks, your solution helped...the only thing I was missing is `.value` I didn't even need the document.oForm... – Nasir May 03 '11 at 11:20
  • @Nasir you really do not need. However, some people consider it a bad practice to get the form variable from the global scope. Glad to help! – brandizzi May 03 '11 at 16:03
0

Try this code out:-

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            return false
         }

         if (str.indexOf(" ")!=-1){
            return false
         }

         return true                    
    }

function ValidateForm(){
    return echeck(document.oForm["email"].value);
 }

And finally just call ValidateForm() and whatever return value you get that is the validity of the email!

Cheers! :D

Axe
  • 6,285
  • 3
  • 31
  • 38