Reputation: 2085
I got this regex check for Email ID from MSDN. But it fails when _ and @ comes one after the other.
Expression:
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
Example: [email protected] fails even though it is a valid ID.
Where do I need to alter the string, it looks really messy and I do not understand it.
Upvotes: 2
Views: 1003
Reputation: 957
Yeah you are right it is not yet available in windows Phone 8. You can use something like below to validate email:
using System;
using System.Text.RegularExpressions;
public static bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn,
@"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
+ "@"
+ @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$");
}
Upvotes: 0
Reputation: 211
You can use this : "\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*". I hope this will work.
Upvotes: 0
Reputation: 174766
Change (?<=[0-9a-z])@
in your regex to (?<=[0-9a-z_])@
(?<=[0-9a-z])@
asserts that the match @
must be preceded by 0-9
or a-z
.
So by adding _
inside the char class would also allow _
to be present before @
.
Assertion only not enough, we have to add real chars (ie, the pattern which actually matches the underscore symbol). Don't worry, \w
inside the char class which exists before lookbehind + @
will do it for you.
Upvotes: 4
Reputation: 2646
This is what i am using :
mRegex = new Regex(@"^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$");
It works fine
Upvotes: 1
Reputation: 957
It is because of this thing in your regex
(?<=[0-9a-z])@
requires explicitly a letter or a digit before the "@".
It is better to use Microsoft EmailAddress Validation attribute to validate email address rather than Regex. Or use this function to validate email address on server side.
public bool IsValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
Upvotes: 0