Validate multiple valid emails separated by semicolon
There are various ways to validate them
1. Using custom validator and calling the javascript
<asp:CustomValidator ID="custValEmail" runat="server" ClientValidationFunction="validateMultipleEmailsCommaSeparated" ErrorMessage="Invalid email(s)"></asp:CustomValidator>
<script language="JavaScript" type="text/javascript">
function validateEmail(field)
{
var regex=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var matchArray=field.match(regex);
if(matchArray==null)
return false;
else
return true;
}
function validateMultipleEmailsCommaSeparated(source, value)
{
var result = document.getElementById("<%= txtEmail.ClientID %>").value.split(';');
for(var i = 0;i < result.length;i++)
{
if(!validateEmail(result[i]))
{
value.IsValid = false;
return false;
}
}
value.IsValid = true;
}
</script>
2. Directly defining the regular expression in the RegularExpressionValidator control
<asp:RegularExpressionValidator ID="s" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid email(s)"
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})(\]?)(\s*;\s*|\s*$))*"></asp:RegularExpressionValidator>
There are various ways to validate them
1. Using custom validator and calling the javascript
<asp:CustomValidator ID="custValEmail" runat="server" ClientValidationFunction="validateMultipleEmailsCommaSeparated" ErrorMessage="Invalid email(s)"></asp:CustomValidator>
<script language="JavaScript" type="text/javascript">
function validateEmail(field)
{
var regex=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var matchArray=field.match(regex);
if(matchArray==null)
return false;
else
return true;
}
function validateMultipleEmailsCommaSeparated(source, value)
{
var result = document.getElementById("<%= txtEmail.ClientID %>").value.split(';');
for(var i = 0;i < result.length;i++)
{
if(!validateEmail(result[i]))
{
value.IsValid = false;
return false;
}
}
value.IsValid = true;
}
</script>
2. Directly defining the regular expression in the RegularExpressionValidator control
<asp:RegularExpressionValidator ID="s" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid email(s)"
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})(\]?)(\s*;\s*|\s*$))*"></asp:RegularExpressionValidator>