Regex email verification error - using JavaScript - javascript

//if HTML5 input email input is not supported
if(type == 'email'){
if(!Modernizr.inputtypes.email){
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\#([a-zA-Z0-9\-])+\.+([a-zA-Z0-9]{2,4})+$/;
if( !emailRegEx.test(value) ){
this.focus();
formok = false;
errors.push(errorMessages.email + nameUC);
return false;
}
}
}
This is my javascript regex for checking if e-mail format is correct. But When I try it myself it shows no error for any ..#.. It does not check .com or whatever in the end. What am I doing wrong?

You need to use a regex that actually fits for an email address. Your current one is completely broken as there are tons of valid addresses it won't accept.
See http://www.regular-expressions.info/email.html for a more detailed description and arguments why a regex is not such a good idea after all.
Anyway, here's one that probably fits for your needs:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Here's what regular-expressions.info says about this regex:
We get a more practical implementation of RFC 2822 if we omit the syntax using double quotes and square brackets. It will still match 99.99% of all email addresses in actual use today.

Related

Regex for Email ([A-Za-z]{2,4})$/

I was reading this link to understand regex:
https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html
In my code, Here is what I have:
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
I was testing this email: "test#tatsu.works", and I got error "Please Enter Valid Email ID"
If I understand correct, it is related to the ending section .([A-Za-z]{2,4})$/
I though the {2,4} means 2 to 4 characters, so I changed to {2,6}, but still getting error.
Basically the ".works" can be any number of characters.
Can I know what I do wrong?
Kind Regards
=================================================================
EDIT:
Apparently, changing to {2,6} or {2,} works, but the reason why it is not working for me is because I am using Chrome and somehow the javascipt is not refreshed (I think related to cookie)
When I switched to Edge, the regex works.
Anyway Thanks to user Wiktor Stribiżew
Parsing emails for validity using a regex is a non trivial problem.
These are all valid emails that your regex will fail
'Long live the king'#com
This\ is\ my##email.address
Fred\#barney#bedrock.gov
See https://beesbuzz.biz/code/439-Falsehoods-programmers-believe-about-email and look up the bits about email addresses.
I'd just use filter_var with the FILTER_VALIDATE_EMAIL flag to check, if an email address is in the correct format.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email address
}

JQuery/javascript email validation with regex [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have been searching around a bit for a good regex for email validation, but for most of the ones I am finding I see the people comment saying the regex is outdated, or it doesn't work... so I am hoping that someone can help me out with an email validation regex that is currently valid for all emails...
here's what I have so far : I have seen people saying that emailReg2 is outdated and produces false positives, but haven't seen anything about emailReg1 being outdated.
var emailReg1 = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,6})?$/;
var emailReg2 = /^([a-zA-Z0-9_.+-])+\#(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
function IsEmail(email) {
var regex = //which regex do I put here?
return regex.test(email);
}
any clarification is greatly appreciated. Thanks!
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
return re.test(email);
}
For a regex to validate email addresses, you should look at your needs and determine if you do need something complex or something very simple.
For a simple (and hackish) regex:
.+?#.+?\..+
Going indepth:
.+? # At least 1 or more character, non-greedy; matches everything before the # sign
# # literal #
.+? # Same as above; matches everything before the .
\. # literal .
.+ # match everything after
This doesn't work for domains like appname.appspotmail.com as it has multiple fullstops in it, but should suit your needs.
You can modify the above regex to accept such conditions. As mentioned, it all depends on your needs. There is no better or worse regex for email validation; there is only a regex that meets your needs perfectly and a regex that is unnecessarily verbose.
If possible, you should come up with your own regex instead of using others, as you're the only person who will know your own needs.
If you must use something that complies with the RFC, look at this extremely verbose regex: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html However, I would discourage you from using that regex and instead look for a library that validates email addresses. You probably don't need to comply with the RFC, though.
If you want resources for debugging regular expressions, give http://regex101.com a try.
The REGEX I use for Email validation is
^[a-zA-Z][\w\.-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$
There are a number of REGEX questions on SO for the Best Email Validation.
Best Regular Expression for Email Format Validation with ASP.NET 3.5 Validation
Validate email address in JavaScript?
Using a regular expression to validate an email address
From most of my research you will find plenty of REGEX that will validate most email addresses, but I have never found one that will catch all emails. You have to find the one that fits your needs the best.

regular expression with if statements

I have this as my regular expression:
var email = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
And this is my if statement:
if($('#email').val() ==""){
$('#emailErrorMsg').text("Please enter a valid email address.");
}
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
else($('#emailErrorMsg').text("Please enter a valid email address."));
});
When I type in a valid email address it says "OK". However, if I enter just some text for example it still says "OK" when I want it to say "Please enter a valid email address". Anyone any idea. By the way, I'm still an amatuer at this stuff!
The main problem is that you have a ? at the end of the regex, following parentheses that enclose the entire pattern. This effectively makes the entire match optional, so the regex will literally match anything.
Note also that you are testing the literal string #email, not the value of the #email element. Make sure you pass the appropriate string to test().
I see that you have jquery tag, so take a look to JQuery validate plugin, it will be better than a simple regex.
But if you still want regex, see Validate email address in JavaScript?
Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html which probably isn't what you want.
Instead, you have a few options. Use a regex that matches 99% of emails, do it server side with an email validation library, or implement a finite state machine to parse it correctly. The state machine is probably too bulky (although allows neat stuff like suggestions for possible typos) and doing it all server side -- which you better be doing anyway (what if someone has JavaScript disabled?) -- loses the benefits of as-you-type checking.
That leaves a simpler regex that doesn't match all legal emails, but matches enough that the chances of someone registering with one that it doesn't are really slim.
The regex from Validate email address in JavaScript? should do the trick pretty well:
/^(([^<>()[\]\\.,;:\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,}))$/
Also, you made a small typo:
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
is testing against the string '#email' -- not the element with the ID 'email'. Change that to:
else if(!email.test($('#email').val())) {
$('#emailErrorMsg').text("OK");
}
There's a little typo in your regex. Try this:
var email = /^([\w-\.]+)#([\w-]+\.)+[\w-]{2,6}?$/;
That should also handle the .museum case

Email regex in JS?

I have a regex that is supposed to match email addresses.
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
When I run the below code in my javascript, it returns null. Could it be an issue with my JS syntax, or is it an issue with the regex?
alert(emailString.match(regex));
This regular expression does not include lowercase letters.
Try this:
^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
Most probably you've forgotten to set the case-insensitive option.
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
And of course, you're missing lots of valid addresses (.museum etc.)...
The problem with using regex to validate emails is even the expression that is the "standard" misses completely valid addresses. You would be far better off checking to see if it contains the # symbol and a . . Or to be really fancy you can poll the email address and if no response is given mark it as invalid, this of course comes with an overhead.
Posting a bit late but this regex works 100% across all email formats.
let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
console.log(rEmail.test("john#gmail.com")); //true
console.log(rEmail.test("john#gmail.com123")); //false
console.log(rEmail.test("john.something#gmail.com")); //true
console.log(rEmail.test("john123#gmail.com")); //true
I assume you have not specified the case-insensitive modifier:
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
// ^
Otherwise the expression only matches upper case letters.
You might want to see RFC 5322, in particular Section 3.4.1
You can use this one. it will support after [dot] 2 ,3 character as
per your domain
var email_filter = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (email_filter.test('yourEmail#gmail.com')) {
alert('Valid Email');
}

JavaScript/Jquery RegEx to validate an EMAIL Address isn't working correctly

I'm using the following lines of JavaScript to validate an input as a valid email address:
// Check for a valid email address
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = $('#formRegister-emailAddress').val();
if(reg.test(address) == false) {
// Validation Failed
alert('Please enter a valid email address.');
}
This is working fantastic for most emails but it doesn't support wildcards like jerry+newsite#yahoo-inc.com but does support jerry#yahoo-inc.com
Any regex experts know if the regex used above can be updated to safely support wildcards?
Thanks
try this regex
/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
try it here
ripped from jQuery-Validate plugin
you are pretty clearly underestimating the problem.
See http://www.regular-expressions.info/email.html Especially the regexes towards the bottom of the page:
(?:[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])+)\])
or
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
That said, they're still not totally rfc 2822 compliant

Categories