There was client requirement with an email validation which should satisfy the following conditions
1. The following characters/formats will not be allowed in the Email field:
• Multiple @ symbols (@@)
• Period followed by an @ symbol (.@)
• The @ symbol followed by a period (@.)
• Multiple periods in sequence (..)
• Any spaces
2. The username portion cannot exceed 64 characters
The following characters are allowed for username (before the @):
• Uppercase and lowercase English letters (a–z, A–Z) (ASCII: 65–90, 97–122)
• Digits 0 to 9 (ASCII: 48–57)
• The following special characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
• Character (dot, period, full stop) (ASCII: 46)
• The dot (.) cannot be the first or last character and it does not appear two or more times in sequence
3. The domain name may have a maximum of 240 characters
The following characters are allowed in the domain name (after the @):
• Uppercase and lowercase English letters (a–z, A–Z) (ASCII: 65–90, 97–122)
• Digits 0 to 9 (ASCII: 48–57)
• Hyphen
• Character (dot, period, full stop) (ASCII: 46)
• The dot (.) cannot be the first or last character and it does not appear two or more times in sequence
4. The entire Email address cannot exceed 255 characters
5. Only one entry of a single Email address may be entered into an Email field
The Regular expression i have used is "\w+([-+.'!#$%&*/=?^_`{|}~]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Using this expression it satisfies all the condition except the length part for local and domain.
<asp:TextBox ID="txtEmailAddress1" runat="server" CssClass="text" Width="250px" MaxLength="255"
TabIndex="44" AutoCompleteType="Disabled" onchange="javascript:return fnEmailValidation();"></asp:TextBox>
<img id="img1" runat="server" src="Images/mail_icon.gif" style="border: 0" />
<asp:RegularExpressionValidator ID="rvEmail1" runat="server" ControlToValidate="txtEmailAddress1"
CssClass="labelmessage" Display="Dynamic" ValidationExpression="\w+([-+.'!#$%&*/=?^_`{|}~]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="True"></asp:RegularExpressionValidator>
To restrict the length of local and domain(local@domain) i had used a javascript function
function fnEmailValidation()
{
var txtemail = document.getElementById("<%= txtEmailAddress1.ClientID%>").value;
var str = txtemail.split('@');
var a = str[0];
var b = str[1];
if(a.length > 64 || b.length > 240)
{
var txtemail1 =document.getElementById("<%= rvEmail1.ClientID%>");
txtemail1.style.display = 'inline';
}
else
{
return true;
}
}
As you can see in the above function, i had used the split property with @ to get the local and domain part separately.I am invoking the regular expression error message for validation.
Hope this is useful to all the developers in future