0

Please see the Javascript code below. The else if block which is doing a check for email pattern is not allowing any of the email ids . What does the match() function return? Please help.

Used test()

empty field :working fine wron mail id : working fine Correct email id : not working

var pattern = new RegExp("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/");
   if(!accountantEmail){
       $("#infoTextMsg").hide();
       $("#accountantEmailNoDataErr").show();
       $("#accountantEmailInvalidFormat").hide();
       $("#accountant_email").focus();
       return false;
   }
   else if(!(pattern.test(accountantEmail))){
       $("#accountantEmailInvalidFormat").show();
       $("#infoTextMsg").hide();
       $("#accountantEmailNoDataErr").hide();
       $("#accountant_email").focus();
       return false;
   }
  • 1
    This might help: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – BillRobertson42 Feb 19 '14 at 13:24
  • 1
    What exactly do you want to validate? That they've entered something that looks like an e-mail address or that they've actually entered a working e-mail address that they have access to? If it's the former, use a regular expression. If it's the latter, check it has an `@` symbol in it then send a verification e-mail for them to confirm it actually works. – Anthony Grist Feb 19 '14 at 13:31
  • anthony , it is showing invalid email id inspite of me entering a valid one – Karthik A Kulkarni Feb 19 '14 at 13:59
  • Your regex will reject a lot of valid address, have a look at: http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses – Toto Feb 19 '14 at 14:58
  • 1
    TLDs may have much more than 3 character long. Here is the list of TLDs: http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Toto Feb 19 '14 at 15:00
  • M42 , at least something like "kulkarni.karthik@gmail.com " should succeed isn't it – Karthik A Kulkarni Feb 19 '14 at 15:21

2 Answers2

0

Javascript match returns an array containing the matches.

Here's the regular expression I use:

var pattern = "[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}";

if(!(accountantEmail.match(pattern))) {
    return false;
}
mayurc
  • 267
  • 4
  • 13
0

For validation scenarios, you should use the RegExp#test function.

var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if (!pattern.test(accountantEmail)) {
    $("#accountantEmailInvalidFormat").show();
    $("#infoTextMsg").hide();
    $("#accountantEmailNoDataErr").hide();
    $("#accountant_email").focus();
    return false;
}

As commented on the other posts, the match function is intended for group capturing.

Also note that you were specifying your pattern with an / on it's beginning. This isn't necessary if you're specifying a RegExp as a string.

everton
  • 7,579
  • 2
  • 29
  • 42
  • I tried this but when i enter an email id it gets returned from the first if block and always gives me "Please enter a mail id "message which i have mostly coded for null validation – Karthik A Kulkarni Feb 19 '14 at 13:44
  • Try changing your first `if` condition to: `if (!accountantEmail){` – everton Feb 19 '14 at 13:48
  • Have you debugged your javascript to check the `accountantEmail` value? It might be really empty for some reason. – everton Feb 19 '14 at 14:05
  • TypeError: accountantEmail.test is not a function says the chrome console – Karthik A Kulkarni Feb 19 '14 at 14:29
  • No, check my snippet again. The `test` function belongs to the RegExp object, not your string. – everton Feb 19 '14 at 14:36
  • (pattern.test(accountantEmail) should be the syantax . I am so dumb :) – Karthik A Kulkarni Feb 19 '14 at 14:43
  • the code looks like this now . If i give a valid email id it says please enter a valid one . var pattern = new RegExp(""); if(!accountantEmail){ $("#infoTextMsg").hide(); $("#accountantEmailNoDataErr").show(); $("#accountantEmailInvalidFormat").hide(); $("#accountant_email").focus(); return false; } else if(!(pattern.test(accountantEmail))){ $("#accountantEmailInvalidFormat").show(); $("#infoTextMsg").hide(); $("#accountantEmailNoDataErr").hide(); $("#accountant_email").focus(); return false; } – Karthik A Kulkarni Feb 19 '14 at 15:15
  • Again... what is your `accountantEmail` variable value? – everton Feb 19 '14 at 15:58