Regular expression to validate comma separated email addresses - regex

I am supposed to validate comma separated email addresses and avoid invalid patterns like
email..email#domain.com,
.email#domain.com,
email#domain.web,
email.#domain.com,
email#-domain.com,
email#domain.web,
email#111.222.333.44444
currently I am using following Regular expression
regex = /^((\w+([-+.']\w+)*#\w+([-.]\w+)*\.([a-zA-Z])+([-.]\w+)*)*([,])*)*$/
(custom regex rule as in validation-engine)
For which I can not use email#domain99.com which can be a valid email address in my case
Please suggest me suitable answer!
EDIT- regex = /^((\w+([-+.']\w+)*#\w+([-.]\w+)*\.([a-zA-Z])+([-.]\w+)*)* ([,])*)*$/ this expression miserably failed when I used ,, instead of , to separate the values. Suggest a way please.

Related

Regular Expression for multiple specific-domain email addresses (separated)

Good day,
I'm looking for a regular expression that would validate the following email addresses:
a#domain.com, b#domain.com
So far, I have this:
^([\w+-.%]+#domain\.com,?\s*)+$
It should not return anything in case of:
a#domain.comb#domain.com or
a#domain.com b#domain.com
It should also return a result if a ; is entered.
Also, is there a way to ensure a result will be returned only when
there is a single # in an address?
Any help would be appreciated.
The regex you are looking for is ^(?>\b[\w+-.%]+#domain\.com(?:[,;]\s*)?)+\b$.
I am using atomic grouping with (?>...) so that no backreferences are created.
Result is as you request: a#domain.com, b#domain.com
Also tested with Expresso.

Regular expression validation contain .com at the end of Email

I am working on the Regular Expression validation and validate on the email textbox.
But the condition is use enter valid email but at the end of email use .com only i use this
ValidationExpression="^\w+([-+.']\w+)*#abc.com$" it works but only for abc at the end of # .
But i need that use will enter any number or alphabet after # .
Thanks
You should not validate email addresses using regular expressions. You will most probably get it wrong.
To validate whether a string ends with .com, use this regex:
\.com$
That's it. Example in C#:
if (Regex.IsMatch(eMail, #"\.com$", RegexOptions.IgnoreCase))
But then, in C#, you could just write
if (eMail.EndsWith(".com", StringComparson.InvariantCultureIgnoreCase))
But i need that user will enter any number or alphabet after #
ValidationExpression = "^\w+([-+.']\w+)*#[A-Za-z\d]+\.com$"

Modify Regex to Also Match IP Address

I currently use this regular expression to validate URLs:
^([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?([a-z0-9-.]*)\.([a-z]{2,4})(\:0*(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9]))?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?$
It matches a fairly long list of URLs:
google.com
google.com#a1
google.com?abc=123
google.com:80
google.com:80#a1
google.com:80?abc=123
google.com:80/test
google.com:80/test#a1
google.com:80/test?abc=123
google.com:80/test?abc=123#a1
www.google.com
www.google.com#a1
www.google.com?abc=123
www.google.com:80
www.google.com:80#a1
www.google.com:80?abc=123
www.google.com:80/test
www.google.com:80/test#a1
www.google.com:80/test?abc=123
www.google.com:80/test?abc=123#a1
www.www.google.com
www.www.google.com#a1
www.www.google.com?abc=123
www.www.google.com:80
www.www.google.com:80#a1
www.www.google.com:80?abc=123
www.www.google.com:80/test
www.www.google.com:80/test#a1
www.www.google.com:80/test?abc=123
www.www.google.com:80/test?abc=123#a1
john:smith#google.com
john:smith#google.com#a1
john:smith#google.com?abc=123
john:smith#google.com:80
john:smith#google.com:80#a1
john:smith#google.com:80?abc=123
john:smith#google.com:80/test
john:smith#google.com:80/test#a1
john:smith#google.com:80/test?abc=123
john:smith#google.com:80/test?abc=123#a1
john:smith#www.google.com
john:smith#www.google.com#a1
john:smith#www.google.com?abc=123
john:smith#www.google.com:80
john:smith#www.google.com:80#a1
john:smith#www.google.com:80?abc=123
john:smith#www.google.com:80/test
john:smith#www.google.com:80/test#a1
john:smith#www.google.com:80/test?abc=123
john:smith#www.google.com:80/test?abc=123#a1
john:smith#www.www.google.com
john:smith#www.www.google.com#a1
john:smith#www.www.google.com?abc=123
john:smith#www.www.google.com:80
john:smith#www.www.google.com:80#a1
john:smith#www.www.google.com:80?abc=123
john:smith#www.www.google.com:80/test
john:smith#www.www.google.com:80/test#a1
john:smith#www.www.google.com:80/test?abc=123
john:smith#www.www.google.com:80/test?abc=123#a1
However, it does not match these URLs which, to my knowledge, are also valid:
8.8.8.8
8.8.8.8#a1
8.8.8.8?abc=123
8.8.8.8:80
8.8.8.8:80#a1
8.8.8.8:80?abc=123
8.8.8.8:80/test
8.8.8.8:80/test#a1
8.8.8.8:80/test?abc=123
8.8.8.8:80/test?abc=123#a1
john:smith#8.8.8.8
john:smith#8.8.8.8#a1
john:smith#8.8.8.8?abc=123
john:smith#8.8.8.8:80
john:smith#8.8.8.8:80#a1
john:smith#8.8.8.8:80?abc=123
john:smith#8.8.8.8:80/test
john:smith#8.8.8.8:80/test#a1
john:smith#8.8.8.8:80/test?abc=123
john:smith#8.8.8.8:80/test?abc=123#a1
For reference, I found this one for IP addresses which seems to work well:
^(?:(?: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]?)
How can I tie them together? Or, is there a better regex to match all the URLs here?
Demo:
http://rubular.com/r/ufuNkHqX5G
You can combine two regular expressions together by doing (?:<regex1>|<regex2>), which means whatever matches regex1 or regex2. (The ?: means the added parentheses won't capture).
You can find a variety of regexes for URL validation online, e.g. In search of the perfect URL validation regex lists quite a few.
Validating an email address is a bit more complicated than validating a webpage URL. In fact,
determining a proper regex for validating an email address seems to be a question without one definitive right answer; see Using a regular expression to validate an email address
If you use PHP, you aren't limited to using a regex to validate email addresses and URLs, as the following code illustrates:
<?php
$url = "http://8.8.8.8";
$mess = (!filter_var($url, FILTER_VALIDATE_URL))? "invalid" : "valid";
echo $mess, ": $url\n";
$email = "me#he re.com";
$mess = (!filter_var($email, FILTER_VALIDATE_EMAIL))? "invalid" :"valid";
echo $mess, ": $email\n";

regular expression attribute in MVC3

How can I use regular expression attribute in MVC3 on EMAIL field to give an error message if the email entered contains no-email.com?
The exact syntax will depend on the language you are using and possibly the method you are using. These examples should help.
You wouldn't normally need a regular expression to match a simple string.
But, if for some reason, it has to be regex, you would just need to escape the hyphen and dot. Like so:
no\-email\.com
Depending on what you are doing, you may need to match the rest of the email address:
(.*?)no\-email\.com
You may also want to tie "no-email.com" to the end of the string, like so:
(.*?)no\-email\.com$
If you also want to match the # sign to the domain name, do:
(.*?)#no\-email\.com$

Regular Expression for some email rules

I was using a regular expression for email formats which I thought was ok but the customer is complaining that the expression is too strict. So they have come back with the following requirement:
The email must contain an "#" symbol and end with either .xx or .xxx ie.(.nl or .com). They are happy with this to pass validation. I have started the expression to see if the string contains an "#" symbol as below
^(?=.*[#])
this seems to work but how do I add the last requirement (must end with .xx or .xxx)?
A regex simply enforcing your two requirements is:
^.+#.+\.[a-zA-Z]{2,3}$
However, there are email validation libraries for most languages that will generally work better than a regex.
I always use this for emails
^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}" +
#"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
#".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Try http://www.ultrapico.com/Expresso.htm as well!
It is not possible to validate every E-Mail Adress with RegEx but for your requirements this simple regex works. It is neither complete nor does it in any way check for errors but it exactly meets the specs:
[^#]+#.+\.\w{2,3}$
Explanation:
[^#]+: Match one or more characters that are not #
#: Match the #
.+: Match one or more of any character
\.: Match a .
\w{2,3}: Match 2 or 3 word-characters (a-zA-Z)
$: End of string
Try this :
([\w-\.]+)#((?:[\w]+\.)+)([a-zA-Z]{2,4})\be(\w*)s\b
A good tool to test our regular expression :
http://gskinner.com/RegExr/
You could use
[#].+\.[a-z0-9]{2,3}$
This should work:
^[^#\r\n\s]+[^.#]#[^.#][^#\r\n\s]+\.(\w){2,}$
I tested it against these invalid emails:
#exampleexample#domaincom.com
example#domaincom
exampledomain.com
exampledomain#.com
exampledomain.#com
example.domain#.#com
e.x+a.1m.5e#em.a.i.l.c.o
some-user#internal-email.company.c
some-user#internal-ema#il.company.co
some-user##internal-email.company.co
#test.com
test#asdaf
test#.com
test.#com.co
And these valid emails:
example#domain.com
e.x+a.1m.5e#em.a.i.l.c.om
some-user#internal-email.company.co
edit
This one appears to validate all of the addresses from that wikipedia page, though it probably allows some invalid emails as well. The parenthesis will split it into everything before and after the #:
^([^\r\n]+)#([^\r\n]+\.?\w{2,})$
niceandsimple#example.com
very.common#example.com
a.little.lengthy.but.fine#dept.example.com
disposable.style.email.with+symbol#example.com
other.email-with-dash#example.com
user#[IPv6:2001:db8:1ff::a0b:dbd0]
"much.more unusual"#example.com
"very.unusual.#.unusual.com"#example.com
"very.(),:;<>[]\".VERY.\"very#\\ \"very\".unusual"#strange.example.com
postbox#com
admin#mailserver1
!#$%&'*+-/=?^_`{}|~#example.org
"()<>[]:,;#\\\"!#$%&'*+-/=?^_`{}| ~.a"#example.org
" "#example.org
üñîçøðé#example.com
üñîçøðé#üñîçøðé.com