3

I am new to javaScript and the problem I am facing is that I have to match the @ symbol and make sure it is allowed only once in a string. To do so I have written the following regex.

var regexpat=/[@]{1}/;
    if(regexpat.test(valu))
       testresults = true;
       else 
      {
            alert("Please input a valid email address!");
            testresults = false;
      }

My regex is working for the following input value: abcd@abc@abc.com. However, if I provide the input value as "abcd@"@abc.com it is not throwing the alert error message.

How can I change my regex so that it will work for "abcd@"@abc.com?

user46777
  • 33
  • 4
  • If you're verifying an email address I suggest you search SO and you'll find lots of regex'es. – SamWhan Sep 05 '17 at 11:46
  • 1
    `valu.replace(/[^@]+/g, "").length == 1` ([see here](https://stackoverflow.com/questions/2903542/javascript-how-many-times-a-character-occurs-in-a-string)) – Wiktor Stribiżew Sep 05 '17 at 11:47
  • Something like (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) – Manav Sep 05 '17 at 11:47
  • However, the regex for what you're describing (to use with `test`) is `/^[^@]+@[^@]+$/`. – SamWhan Sep 05 '17 at 11:49
  • How on earth is it a valid js? – revo Sep 05 '17 at 11:59

3 Answers3

2

Your regexp just tests whether there's a single @ in the string, it doesn't reject more than one. Use:

var regexppat = /^[^@]+@[^@]+$/;

This matches an @ that's surrounded by characters that aren't @.

var valu;
var regexpat = /^[^@]+@[^@]+$/;

while (valu = prompt("Enter email")) {
  if (regexpat.test(valu))
    console.log(valu + " is valid");
  else {
    console.log(valu + " is invalid");
  }
}
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • Hi Barmar,but for the input with quotes like "abc@"@abc.com for this it should go to the else loop.I used your regex then also it's not going to the else loop. – user46777 Sep 05 '17 at 12:12
  • I've added an executable snippet that shows it works. Enter `"abc@"@abc.com` into the prompt. – Barmar Sep 05 '17 at 12:23
0

The easy way could also be to use the split("@") for this:

var value = 'abcd@@';
 if(value.split("@").length === 2){
  testresults = true;
 }else{
   alert("Please input a valid email address!");
   testresults = false;
 }     
      

Just split your string with @ and since you require only one occurrence of @ there must be an array of length 2 so you can compare the array with length 2. If the array length is greater than 2 then there are more than one occurrence of @

Ankit Agarwal
  • 29,658
  • 5
  • 35
  • 59
0

E-Mail regex is much more, than just the occurence of just one @ character. This is the email-regex specified in the W3C Spec (e.g. used for <input type="email">):

/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
MattDiMu
  • 4,628
  • 1
  • 17
  • 28