RegularExpressionValidator for multiple emails - asp.net

I'm validating a textbox for valid email with this:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtMailCustom"
Text="Invalid address"
ValidationExpression="^([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})(\]?)$" runat="server" />
Now I want users to be able to put multiple email addresses , separated by a comma and space.
How can I integrate that behaviour ?

Try the expression below, which works for me:
((\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)*
The above code is for ,, separating e-mail addresses.
If you would like to use ; instead of ,, than replace , with ; at the end of the above expression.

try this expression:
^(\s*,?\s*[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})+\s*$

I should do this: what it does is check for at least one mail, and for list of email before it with each a comma and possibly a space.
^(([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})(\]?), ?)*^([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})(\]?)$
Use this if the space between each mail is required:
^(([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})(\]?), )*^([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})(\]?)$
Note I just used your single mail checker as a base and didn't really edit it.

Related

RegEx stops working when placed into ValidationExpression

I have a text box where a user enters their email address. I need to prevent people using certain email addresses, as this is for corporate (B2B) use.
Can anyone help me with the RegEx which would return false if email addresses contain #gmail or #yahoo?
So far I have this (thanks to #Sabuj) #(yahoo|gmail)\. but when placed into a RegularExpressionValidator it doesn't work:
<asp:RegularExpressionValidator ValidationExpression='#(yahoo|gmail)\.' runat="server" ControlToValidate="txt_email" />
Having read MSDN for more info, I've also tried this but it still returns true regardless of the content entered:
<asp:RegularExpressionValidator ValidationExpression='^(#(yahoo|gmail)\.)$' runat="server" ControlToValidate="txt_email" />
Since e-mail addresses have a complex syntax (more complex than most people realise, for instance, they can contain comments [RFC 822 ยง 3.4.3]), I'd suggest not using regex at all for this. Instead, use a "proper" e-mail parser, then ask the parser for the domain part of the address.
Use this:
<asp:RegularExpressionValidator ValidationExpression=".*#(?!(yahoo|gmail)).*" ControlToValidate="txt_email" runat="server" ErrorMessage="Yahoo and Gmail disallowed"></asp:RegularExpressionValidator>
The validation expression property should be set to match the entire string.
But my regex .*#(?!(yahoo|gmail)).* matches the whole email. So it works :)
You don't need ^ or $ since the string is gonna be a single line.
Also don't forget to add type="email" to your txt_email. It will automatically take care of whether it is a valid email or not.
If the error msg appears, then it isn't valid, but if it doesn't appear, then it is absolutely valid.
I've come up with ^.*#(?!(yahoo|gmail)).*$
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="^.*#(?!(yahoo|gmail)).*$" runat="server" ControlToValidate="txt_email" Text="No free email accounts allowed" />
This will allow any text to pass the validator that doesn't contain #yahoo or #gmail.
Don't forget to check Page.IsValid in your code behind, and to include an <asp:ValidationSummary runat="server" /> in your .aspx.
You can use this regex to check whether the mentioned emails are containing or not:
#(gmail|yahoo|mailinator|guerrillamail|dispostable)\.

how to force TextBox to only accept strings

Sorry for the Dummy Question , i know :( ,, but it's only the simple things that dont work with me :((
i have many text boxes and i want the user to only insert String and not Numeric numbers ,
how could i handle it in easy way ??
as it takes every thing and apply it to the database , or should i control it from the Database
PS. i have searched a lot but with no good answer
use [a-zA-Z]+ for ValidationExpression:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="[a-zA-Z]+"></asp:RegularExpressionValidator>
You could take a look at validation techniques for asp : http://msdn.microsoft.com/en-us/library/7kh55542.aspx
This provide a set of tools to check whether the input match what you expect.
You can do it easily in AJAX,just download it from here
first add a script manager to your page then add FilteredTextBoxExtender to the textbox and set it's properties as you wish.
A Regular Expression could be applied to the input
For the basics on RegEx : http://www.regular-expressions.info/tutorial.html
And also see
http://www.regular-expressions.info/dotnet.html
You can use regex to do that with jQuery.
In this example, I replace only digits.
You can adapt the regex to replace any set of characters with an empty string.

Email regular expression validator for allowing whitespace at start and end

Following expression work well for email validation with asp.net but does not allow whitespace at start and end.
^(([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$
Have any way to allow whitespace?
Here is full asp.net markup, I can not trim before validation.
<asp:TextBox ID="txtEmail" runat="server" MaxLength="100" CssClass="textbg text"></asp:TextBox>
<asp:RegularExpressionValidator ID="EmailValidator" runat="server" ErrorMessage="Email is not vaild."
ForeColor="Red" ControlToValidate="txtEmail" ValidationExpression="^(([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$"
ValidationGroup="register">Email is not vaild.</asp:RegularExpressionValidator>
I would echo the previous points suggesting trimming the input prior to validation. Trimming or ignoring whitespace is a pretty common requirement, so I would be very surprised if your validator couldn't handle it.
I would also point out an email address with that spaces on either end is an invalid email address, so passing it as valid is technically incorrect.
However, if it can't trim it, and you must pass it, then it is trivially easy to modify any regex to allow whitespace at either end.
White space is represented in regex with \s. Zero or more white spaces would be \s*.
Therefore, all you need to do is add \s* to the start and end of the expression, immediately inside the^and$` start and end markers, like so:
/^\s*(.......)\s*$/
(where ...... represents an expression to validate an email address (but probably not the one you've quoted in the question).
Hope that helps.
Email validation using regular expressions are not easy. Since you're using ASP.NET, you could just use the same regular expression MailAddress uses. Presumably most email addresses are not going to be invalid, so the Try...Catch operation shouldn't be too expensive.
MailAddress mailAddress;
try
{
mailAddress = new MailAddress(txtEmail.Text);
}
catch (Exception ex)
{
//Email considered invalid
}
Edit 2 - This one should work with the validator:
(\s?)+\w+(([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*(\s?)+
Just be aware, email validation cannot be considered reliable because it's very easy to bypass most of the regular expressions.

Regular expression for e-mail validation not working

I've set up a regular expression validator to validate all e-mail addresses that are entered into a textbox on one of my forms the only problem is it's not working in a weird case.
Here's my code
<asp:TextBox ID="tbEmail1" runat="server" />
<asp:RegularExpressionValidator Display="Dynamic" ID="rgv1" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail1" ErrorMessage="Invalid email address" />
The case that this is not working is when an e-mail address is entered like this
jamie-taylor-#hotmail.co.uk
Now I wasn't aware that e-mail addresses could be set up in this way but i've just set one up and it seems to be fine except the fact that I cannot input this into my form without it telling me it's invalid
Any ideas?
Thanks in advance
Comparing E-mail Address Validating Regular Expressions. This page has both regexes and e-mail addresses to test with.
It is generally better to make your regex not very strict. Better to have one or two e-mails bounce back than that your customers can not create an account.
let's enhance the regex :
\w+([-+.']\w+[-]*)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
edit
for #Sjoerd :
\w+([-+.']\w+[-]*)*#\w+([-.]*\w+)*\.\w+([-.]\w+)*
use the below expression may help you
\w+([-+.']\w+[-])*#\w+([-.]\w+)*\.\w+([.]\w+)*

Email Regex Validator, not allowing "."

Hey I have the following Regex Validator
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="email"
ErrorMessage="Email requires a vaild email address" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
But its not allowing the follow mail e.clear#company.ie , I believe the issue is with the . before the # symbol, but not sure how to update the regex to reflect this
Use this:
\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
This is the default regular expression provided with RegularExpressionValidator. Yours expression have a ' after 8th char. It seems to be invalid.
i think its working for e.clear#company.ie. But you are not given the validation group.

Resources