Validating Multiple E-mail Address - JavaScript & CustomValidator

C

cemcat

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 ([email protected]). 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 ([email protected]). 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!!!
 
S

Siva M

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

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 ([email protected]). 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 ([email protected]). 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!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top