Hello,
We have an ASP.NET 2.0 (C#) web form that contains a textbox for users to
enter multiple e-mail addresses separated by semicolons. We need to
validate that each individual e-mail address entered is a valid e-mail
address format. We've added a CustomValidator to perform this validation.
We have the server-side validation working fine, but now we need to add some
client-side validation via JavaScript. We are having difficulties with
this.
Here's what we have so far:
<asp:TextBox ID="TextBoxTo" runat="server" ToolTip="To"></asp:TextBox>
<asp:CustomVali dator
ID="CustomValid atorTo"
runat="server"
ClientValidatio nFunction="vali dateTo"
ControlToValida te="TextBoxTo"
Display="Dynami c"
ErrorMessage="I nvalid e-mail address or addresses. E-mail address must
be in a standard format (na**@company.c om). Use a semicolon (;) with no
spaces to separate multiple e-mail addresses."
Text="*"
ToolTip="Invali d e-mail address or addresses. E-mail address must be in
a standard format (na**@company.c om). Use a semicolon (;) with no spaces to
separate multiple e-mail addresses."
ValidationGroup ="AllValidators "
OnServerValidat e="CustomValida torTo_ServerVal idate">
</asp:CustomValid ator>
<script type="text/javascript">
function validateTo(oSrc , args)
{
args.IsValid = false;
var formToValidate = document.forms['aspnetForm'];
if (!formToValidat e)
{
formToValidate = document.aspnet Form;
}
var controlIndex;
var numberOfControl s = formToValidate. length;
var element;
for (controlIndex = 0; controlIndex < numberOfControl s;
controlIndex++)
{
element = formToValidate[controlIndex];
if ((element.type == "text") &&
(element.id.las tIndexOf("TextB oxTo") > 0))
{
// First trim any space and/or ; from the front or back of
the text entered in the textbox
// Split the text at every ; to get each individual e-mail
address
// Trim any space and/or ; from each individual e-mail
address
// Use a regular expression to validate each individual
e-mail address
}
}
}
</script>
Can anyone help us figure out the JavaScript code necessary to perform the
validation (see the commented line in the code above)?
Thanks!!!