The CreatorCon Call for Content is officially open! Get started here.

RegExp Email Validation is not working

VK13
Tera Expert

Hello all,

 

I am trying to build a basic regexp for email validation, but it's not working. Could you please what is wrong with the code.

*********************************************************************

var abc=g_form.getValue('email');
    var patt=/^[a-zA-Z0-9]\@[a-zA-Z0-9]\.[a-zA-Z0-9]$/;
     if(!(patt.test(abc)))
        alert('invalid email format');

*********************************************************************

I tested with xyz@gmail.com, but it's showing alert message. I tested by removing escape character "\", yet did not work.

9 REPLIES 9

Hi Kunal,

 

Thanks. But could you please check why my above script is not working when I test it with abc@gmail.com?

var abc="xyz@gmail.com";
    var patt=/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
     if(!patt.test(abc)){
        console.log('valid email format');
     }
     else{
        console.log('Invalid email format');
     }
 
Try this way
 
Thanks
Sudhanshu

asifnoor
Kilo Patron

Hello VK,

Here is the corrected regex of yours.

var patt=/^[a-zA-Z0-9]+@[a-zA-Z0-9]+(.[a-zA-Z0-9]+)*$/;
//for better validation, use this
//var patt=/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

Mark the comment as a correct answer and helpful if it works.

MrMuhammad
Giga Sage

Hi @VK

In your regex ( var patt=/^[a-zA-Z0-9]\@[a-zA-Z0-9]\.[a-zA-Z0-9]$/; couple of issues are spotted.

Let me break down and explain it to you. 

 
^ asserts position at start of the string
 
Match a single character present in the list below 
[a-zA-Z0-9]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9 (case sensitive)
 
\@ matches the character @ literally (case sensitive)
 
Match a single character present in the list below 
[a-zA-Z0-9]
 
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z  (case sensitive)
0-9 a single character in the range between 0 and 9  (case sensitive)
 
\. matches the character . literally (case sensitive)
 
Match a single character present in the list below 
[a-zA-Z0-9]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9 (case sensitive)
 
$ asserts position at end of the string
 

Corrected Regex will be. ( Updates are bold ) 

/^[a-zA-Z0-9]+\@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed 

For example - ^[a-zA-Z0-9]+

Adding will now check one of more characters or digits defined instead of single character.

Refer to https://regex101.com/ for your regex explanation. 

 

I hope this helps!

 

Thanks & Regards,

Sharjeel  

 

Regards,
Muhammad

Hitoshi Ozawa
Giga Sage
Giga Sage

The following regular expression is not representing the email address correctly.

/^[a-zA-Z0-9]\@[a-zA-Z0-9]\.[a-zA-Z0-9]$/

First, you're missing character "+" to represent that there is at least 1 or more characters. As it is, the above expression would only match something like "a@b.c".

To match string, insert the "+" character as below. 

/^[a-zA-Z0-9]+\@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/

However, this is actually still incorrect because email address can contain "-", "_", and "." characters as well. So the regular expression will be as below(character "\" is used to escape the next character because it has a special meaning in regular expression):

/^[a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9_\-\.]+\.[a-zA-Z0-9]+$/

This still isn't correct because the last string that represents domain doesn't contain numbers and have limited length. Taking these into consideration, the regular expression is as follows:

/^[a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}$/

You can test this regular expression using email addresses such as followings:

generator@gmail.com
generato.r@gmail.com
generat.or@gmail.com
generat.o.r@gmail.com
genera.tor@gmail.com
genera.to.r@gmail.com
genera.t.or@gmail.com
g-ernator@gmail.com
g_ernator@gmail.com
g_ernator@gmail.co.jp
g_ernator3@gmail.co.jp