Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 customValidator control

Author  Topic 

-Dman100-
Posting Yak Master

210 Posts

Posted - 2006-08-13 : 11:29:27
I'm using a customValidator control to validate a phone number with three text boxes

[areacode] [prefix] - [4-digit]

These are the controls on my page:

<asp:TextBox ID="areacode" runat="server" MaxLength="3" Width="25px"></asp:TextBox>
<asp:TextBox ID="prefix" runat="server" MaxLength="3" Width="25px"></asp:TextBox> -
<asp:TextBox ID="suffix" runat="server" MaxLength="4" Width="30px"></asp:TextBox> 
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="* Please enter a valid phone number" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

In my code-behind I have created a method to validate the three textboxes:

protected void ValidatePhoneNumber(object source, ServerValidateEventArgs args)
{
String areacodeRegex = "\\d{3}";
String prefixRegex = "\\d{3}";
String suffixRegex = "\\d{4}";
if ((!Regex.IsMatch(areacode.Text, areacodeRegex)
|| (!Regex.IsMatch(prefix.Text, prefixRegex) || Regex.IsMatch(suffix.Text, suffixRegex))))
{
//CustomValidator1.IsValid = false;
args.IsValid = false;
}
}

Then I call the method within the ServerValidateEventHandler:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator1.ServerValidate += new ServerValidateEventHandler(ValidatePhoneNumber);
}

I've also tried calling the method directly:

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="* Please enter a valid phone number" OnServerValidate="ValidatePhoneNumber"></asp:CustomValidator>


I can't get the validation to work. I'm not getting any errors.

The error message isn't displaying. I don't think my statement is wrong.

Can anyone explain where I've gone wrong or what I'm missing? This is the only validation control I can't figure out and get working correctly.

Thanks.
   

- Advertisement -