RegExp Email Validation is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 07:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 09:24 AM
Hi Kunal,
Thanks. But could you please check why my above script is not working when I test it with abc@gmail.com?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 10:13 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 10:11 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 11:35 AM
Hi
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.
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
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 02:27 PM
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