By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

Validating Multiple E-mail Address - JavaScript & CustomValidator

P: n/a
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:CustomValidator
ID="CustomValidatorTo"
runat="server"
ClientValidationFunction="validateTo"
ControlToValidate="TextBoxTo"
Display="Dynamic"
ErrorMessage="Invalid e-mail address or addresses. E-mail address must
be in a standard format (na**@company.com). Use a semicolon (;) with no
spaces to separate multiple e-mail addresses."
Text="*"
ToolTip="Invalid e-mail address or addresses. E-mail address must be in
a standard format (na**@company.com). Use a semicolon (;) with no spaces to
separate multiple e-mail addresses."
ValidationGroup="AllValidators"
OnServerValidate="CustomValidatorTo_ServerValidate ">
</asp:CustomValidator>
<script type="text/javascript">
function validateTo(oSrc, args)
{
args.IsValid = false;

var formToValidate = document.forms['aspnetForm'];

if (!formToValidate)
{
formToValidate = document.aspnetForm;
}

var controlIndex;
var numberOfControls = formToValidate.length;
var element;

for (controlIndex = 0; controlIndex < numberOfControls;
controlIndex++)
{
element = formToValidate[controlIndex];

if ((element.type == "text") &&
(element.id.lastIndexOf("TextBoxTo") > 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!!!
Dec 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You might want to use regular expressions. A sample is here:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=22

"cemcat" <no****@company.com> wrote in message
news:uG**************@TK2MSFTNGP10.phx.gbl...
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:CustomValidator
ID="CustomValidatorTo"
runat="server"
ClientValidationFunction="validateTo"
ControlToValidate="TextBoxTo"
Display="Dynamic"
ErrorMessage="Invalid e-mail address or addresses. E-mail address must
be in a standard format (na**@company.com). Use a semicolon (;) with no
spaces to separate multiple e-mail addresses."
Text="*"
ToolTip="Invalid e-mail address or addresses. E-mail address must be in
a standard format (na**@company.com). Use a semicolon (;) with no spaces to
separate multiple e-mail addresses."
ValidationGroup="AllValidators"
OnServerValidate="CustomValidatorTo_ServerValidate ">
</asp:CustomValidator>
<script type="text/javascript">
function validateTo(oSrc, args)
{
args.IsValid = false;

var formToValidate = document.forms['aspnetForm'];

if (!formToValidate)
{
formToValidate = document.aspnetForm;
}

var controlIndex;
var numberOfControls = formToValidate.length;
var element;

for (controlIndex = 0; controlIndex < numberOfControls;
controlIndex++)
{
element = formToValidate[controlIndex];

if ((element.type == "text") &&
(element.id.lastIndexOf("TextBoxTo") > 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!!!

Dec 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.