Email regex in JS? - javascript

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');
}

Related

Rewrite regex to accept conditional terms

^([a-z0-9_\.-])+#[yahoo]{5}\.([com]{3}\.)?[com]{3}$
this currently matches xxxx#yahoo.com , how can I rewrite this to match some additional domains? for example, gmail.com and deadforce.com. I tried the following but it did not work, what am I doing wrong?
^([a-z0-9_\.-])+#[yahoo|gmail|deadforce]{5,9}\.([com]{3}\.)?[com]{3}$
Thank you in advance!
Your regex doesn't say what you think it says.
^([a-z0-9_\.-])+#[yahoo]{5}\.([com]{3}\.)?[com]{3}$
Says any characters a-z, 0-9, ., - one or more times.
That later part where you are trying match yahoo.com is incorrect. It says y, a, h, or o, any of those characters are allowed 5 times. Same with the com so aaaaa.ooo would be valid here. I'm not sure what the ([com]{3}\.)?[com]{3} was trying to say but I presume you wanted to check for .com.
See character classes documentation here, http://www.regular-expressions.info/charclass.html.
What you want is
^([a-z0-9_.\-])+#yahoo\.com$
or for more domains use grouping,
^([a-z0-9_.\-])+#(yahoo|gmail|deadforce)\.com$
You haven't stated what language you are using so a real demo can't be given.
Functional demo, https://jsfiddle.net/qa9x9hua/1/
Email validation is a notoriously difficult problem, and many people have failed quite horribly at trying to validate them themselves.
Filter var has a filter just for emails. Use that to check for email address validity. See http://php.net/manual/en/function.filter-var.php
if (filter_var('bob#example.com', FILTER_VALIDATE_EMAIL)) {
// Email is valid
}
There's probably no downside to doing the domain check the easy way. Just check for the domain strings in the email address. e.g.
if (
filter_var($email, FILTER_VALIDATE_EMAIL) &&
preg_match("/#(yahoo|gmail|deadforce)\.com/", $email)
) {
// Email is valid
}
In terms of your original regular expression, quite a lot of it was incorrect, which is why you were having trouble changing it.
regexper shows what you've created.
([a-z0-9_\.-])+ should be [a-z0-9_\.-]+ or ([a-z0-9_\.-]+)
The () are only capturing results in this section. If you want results move the brackets, if not remove them.
[yahoo]{5} should be yahoo
That's matching 5 characters that are one of y,a,h,o so it would match hayoo etc.
\.([com]{3}\.)?[com]{3} should be \.com
Dunno what this was trying to accomplish but you only wanted .com
Take a look at http:// www.regular-expressions.info /tutorial.html for a guide to regular expressions

Email regex validation javascript should not contain example.com

I have requirement for email validation that the email should not be with a particular domain,
for example if the email is email#example.com it is invalid. That is the RegExp should exclude the domain example.com
I tried to use like to exclude a string like
^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$.?!example.com
It's not working
Please help me with a RegExp.
Use this regex
(?=.*#)(?!.*example).*
This matches, all emails, that does not come from domain example.com
I've made a function for you.
function isValidEmail(input, excludedDomain) {
alert(new RegExp("(?=.*#)(?!.*" + excludedDomain + ").*").test(input));
}
isValidEmail("hi#example.com", "example"); //false
isValidEmail("hi#hotmail.com", "example"); //true
Demo:http://jsfiddle.net/W3rLU/
The correct Regexp is something like this.
^\w+#(?!example\.com)[a-zA-Z_]+?\.[a-zA-Z]{2,3}$
Debuggex Demo
It means: after the #, check that is not followed by "example.com" and then check that is followed by a character and a two or three letter domain.
I also recommend you to change it a bit to allow domain names with numbers and dashes (e.g. myname#domain-123.com) and subdomains (myname#subdomain.domain.com) with this expression:
^\w+#(?!example\.com)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,6})$
Debuggex Demo

Email validation Javascript RegExp

With this RegExp I can easily check if an email is valid or not:
RegExp(/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/);
However, this just return true for such addresses:
example#example.com
I also want to accept:
*#example.com
What changes I need to apply on my RegExp?
Thanks in advance
To answer your question literally, you can "augment" your regex:
RegExp(/^([\w.*-]+#([\w-]+\.)+[\w-]{2,4})?$/);
But this is a terrible regex for e-mail validation. Regex is the wrong tool for this. Why do you insist on doing it this way?
A couple of things: to accept *#foo.bar:
var expression = /^([\w-\.*]+#([\w-]+\.)+[\w-]{2,4})?$/;//no need to pass it to the RegExp constructor
But this expression does accept -#-.--, but then again, regex and email aren't all too good a friends. But based on your expression, here's a slightly less unreliable version:
var expression = /^[\w-\.\d*]+#[\w\d]+(\.\w{2,4})$/;
There is an expression that validates all valid types of email addresses, somewhere on the net, though. Look into that, to see why regex validating is almost always going to either exclude valid input or be too forgiving
Checking email addresses is not that straightforward, cf. RFC 822, sec 6.1.
A good list of regexes can be found at http://www.regular-expressions.info/email.html, describing tradeoffs between RFC conformance and practicality.

Regex email verification error - using 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.

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation

Categories