regex for email validation Not Working with Subdomains? - javascript

I'm using the following for email validation:
var filter = /^([\w]+)(.[\w]+)*#([\w]+)(.[\w]{2,3}){1,2}$/; // For Email Validation
if (filter.test(emailInputVal))) {console.log('good')}
For some reason the above does not work with emails that have a subdomain Any ideas why?
xxxx#xxx.xxx.com
Thanks

Because your regular expression is incorrect. Try this instead:
var filter = /^\w+(?:\.\w+)*#\w+(?:\.\w+)+$/;
This link may help you lots when validating email addresses:
http://www.regular-expressions.info/email.html
Official RFC 2822 standard
This non-trivial simplified regular expression conforming to RFC 2822 standard:
var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;

That is one weird regex. It's certainly not doing what you're expecting it to do, for example because the dot isn't escaped when you do mean a literal dot.
Since it's impossible to really validate an email address with a regex anyway - why not go for something simpler?
/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/i
This will still match some invalid addresses and will reject some valid addresses (as all readable regexes do), but in the end you have to send a confirmation mail to a user-submitted mail address and see if you get a reply if you truly want to validate it.

You can't reliably validate email addresses with regular expressions. What I'd do:
use a simple expression like /^[^#]+#([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+$/ for client-side validation to catch typos
check the DNS record on the server-side
send a confirmation mail

Your last component is: any length word, then one or two instances of (dot, two-or-three-letter word). I would expect "xxxx#xxx.xxx.com" to work, but perhaps not more realistic examples like "xxxx#xxx.example.com" because your domain name is not a two-or-three-letter word.
Do yourself a favor: use simply /^[^# ]+#[^# ]+\.[^# ]+$/ More about this: http://nedbatchelder.com/blog/200908/humane_email_validation.html

I am not sure if the above code will work any format, because there is an extra ) in the if condition, removing it works for the sub domain too:
if (filter.test(emailInputVal)) {console.log('good')}

Related

RegEx for email to allow Empty spaces, vaild email address and multiple email addresses

I have this RegEx which I use for CC and BCC email fields
reg = /(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)/;
This allows for the email field to be empty, or have a valid email address, otherwise it will error.
I would like to extend the RegEx to allow mutiple emails also e.g. a#a.com, b#b.com, c#c.com
I have tried adding [,;] to allow comma or semicolon seperated values, but i can't seem to get it to work.
Any one know if i'm on the right lines with [,;] and where I should be placing it?
Update: I've updated the RegEx to, so it doesn't look for gTLDs:
reg =
/(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+.[A-Za-z]{2,4}[,;]?)+$/;
thanks
If Alex K.'s comment about ASP.NET validation doesn't help, then I have a band-aid for you. I wouldn't consider this a proper answer, as there really isn't a way to get exactly the functionality that you're looking for without giving us all pre and post email special characters that can occur. You could use something like this that uses non-capture groups to help find matches. It's not 100% accurate, but it should work for most cases. One problem with it is that you're apt to capture garbage/non-desired results if it runs into stray # symbols.
regex tested by RegexBuddy 4.2.0:
(?m)(?:^|\s|\n|\t|\r|,|;|
)[^\n]*?#[^\n]*?\.[^\n]*?(?:$|;|\s|,)
Test strings used:
9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cm3Gks.qa1vv, 9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cms.com ;
dd.dd.ddwe.wscef_sed#_e23&&*^.dvcw

RegExp matching url w/o affect email address

Supposed that I have a block of text like this:
My site: http://www.mysite.com,
drop me an email at foo#bar.com
I want to replace url & email address to convert text to link.
I then used this pattern for email:
text.replace(/([\w\-\+_]+(\.[\w\-\+_]+)*\#[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*)/gi, replacement);
and below pattern for url:
text.replace(/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, replacement);
But the url pattern screw up my email pattern, the last result become like:
bar.com>bar.com</a>
Is there any better pattern for this situation?
Thanks
I would suggest you to go with this regex:
/(\b(?:ht|f)tps?:\/\/.+\b)|(\b[\w.]+#(?=.*?\.)[\w.]+\b)/g
And replace it with:
$1$2
Demo
Well, I think your core problem will be that your 'URL' pattern also contains an '#' so it'll 'match' your email address as well.
However, I would also suggest that your patterns are way too complicated.
Something like:
email pattern should be something like:
[\w.+]+\#[\w.]+
url pattern something like:
https?://[\w.]+
It won't catch some of the more unusual, but otherwise valid patterns that could be used for URLs (that latter for example, won't catch URLs for CGI GETs). Neither does either really validate the data. But you don't want a regex for that in the first place really.

JavaScript Regex E-Mail Validation

I'm really new to regex in general. All I need is it to check and make sure (Something#something.something) works. I've tried this.
var checkEmail = /^\w+#\w+.[a-zA-Z]/;
Is something like this correct for what I'm looking for?
To refine what you have:
var checkEmail = /^\w+#\w+\.[a-zA-Z]+/;
What you posted it close (you should escape the . so it doesn't match any character and add a + after the [a-zA-Z] because top-level domains are at least 2 character I think), but for something like an email address that actually has a long and little known spec, I would just use someone else's regex.
Here's a site with more info:
http://www.regular-expressions.info/email.html
You should escape the dot, otherwise it is a meta character that matches anything. Try:
/^\w+#\w+.[a-zA-Z]/

Find everything that's not an email address using only regex

I need to find everything in a string that is not an e-mail address.
Here is my version of how to find an e-mail address.
^[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})$
I want to modify this regex to find the inverse--everything other than the e-mail address in any string.
###Example 1:
asdasd
###Example 2:
123#asd.com sda
Note: I want to get status == true in the following line:
var status = myString.match(pattern matches everything that is not an email address);
###I can only change the pattern, nothing else!
The official standard is known as RFC 2822. Regex pattern for email address is then:
(?:[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])+)\])
More practical implementation of RFC 2822 (if we omit the syntax using double quotes and square brackets), which will still match 99.99% of all email addresses in actual use today, is:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
To get list of non-matching "words" from myString use JavaScript code:
var status = myString.match(/(?:\s|^)(?![a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\S+\b(?=\s|$)/ig);
Check this demo.
You can call replace, with an empty string to remove the instances of the emails matching your pattern:
var noEmails = stringWithEmails.replace(/[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})/g, '');
Just note that I took out the leading ^ and trailing $. These were forcing the pattern to match the whole string (^ is beginning of line, and $ is end of line).
var result=myString.replace('[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})', '');
string.replace(/[/[a-zA-Z0-9_.-]+#[a-zA-Z0-9][a-zA-Z0-9-.]+.([a-zA-Z]{2,6})]*/gi, '');
its worked
Based on your edits, what you actually want is
var status = ! myString.match(email pattern)
That is, use your email pattern (or the giant thing posted by Ωmega) with the guards. That will match everything that is only an email address. That's exactly the opposite of what you want, so invert the boolean and you're done.

Is the following function ok to validate an email?

Just tested this function to validate an email..However,it does not validate the presence or absence of the dot in the email..Why this is happening and how can I fix this?
<script type="text/javascript">
function isEmail(textbox){
var reg4 =/^(\w+)#(\w+).(\w+)$/;
if(reg4.test(textbox.value)){
return true;
}
return false;
}
</script>
No, it's insufficient.
In particular, it doesn't handle all these (legal) variants:
quoted user parts, e.g. "my name"#example.com
Internationalised domain names (IDNs)
Domains with more than two labels
Domains with only one label (yes, those are legal)
Hyphens in domain names
user#[1.2.3.4]
Validating an e-mail address via a regexp is very hard. See section 3.4.1 of RFC 5322 for the formal syntax definition.
Do not use a regular expression to validate email addresses. Email addresses can include a lot of things you wouldn't imagine (like spaces), and it's more likely that the user will accidentally enter an invalid email address than an invalid one. Just check if there's an # then send a confirmation email if you want.
From an answer to a related question:
There is no good (and realistic, see the fully RFC 822 compliant
regex) regular
expression for this problem. The grammar (specified in RFC 5322) is
too complicated for that. Use a real parser or, better, validate by
trying (to send a message).
the dot has a meaning in a regex
use [\.] instead of .
You need to escape the dot, which normally matches any character.
var reg4 =/^(\w+)#(\w+)\.(\w+)$/;
Escape the dot with backslash: \.. However, your regex would not be very good afterwards as it will not accept multiple dots in post-# part, such as domain foo.co.uk.
All in all, I'd advise againts using regexes to validate emails as they tend to get overcomplicated ;-) Simply check for presence of # followed by a dot, or use a similarly lenient algorithm.

Categories

Resources