Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questions about validators

Status
Not open for further replies.

Garreth21

Programmer
May 19, 2005
29
Hi everyone.

I have a web form that collects shipping information. I have the following fields: First Name, Last Name, Street Address, City, Postal Code, email address. The form needs to accept english and french characters as it's multi-lingual.

I'm trying to validate the First Name field. I tried using a regularexpression validator but it never worked. It always shows the message no matter what is entered. The regular expression works when I uses it in code behind.

This is the code:
Code:
<asp:RegularExpressionValidator id=RegularExpressionValidator1 
runat="server" 
CssClass="text1" 
ErrorMessage="Invalid name. Enter text only." ControlToValidate="TextBox_firstName" ValidationExpression="^\p{L}[\p{L}\x27\\.\s\-]*\p{L}">
</asp:RegularExpressionValidator>

Can anyone see why this might not work?


So after that frustration, I tried a custom control. Which worked except for one problem. I had to call .validate() either on the control or the page, which then called the method twice. If I never called .validate(), the method was never called when I clicked to move on the next page. The button does cause validation so I don’t know why the method isn’t called.


Here is the code for the custom:
Code:
code behind
public void ValidateFirstName_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) {
Regex rx = new Regex("^\p{L}[\p{L}\x27\\.\s\-]*\p{L}");
Match m = rx.Match(args.Value);
			
if(!m.Success) {
args.IsValid = false;
}
}

web form
<asp:CustomValidator 
id=ValidateFirstName 
runat="server" 
CssClass="text1" 
ErrorMessage="Invalid name. Enter text only." ControlToValidate="TextBox_firstName" OnServerValidate="ValidateFirstName_ServerValidate">
</asp:CustomValidator>


How can I use the custom control to validate the first name textbox without the method being called twice?


Any help or advice is appreciated.
 
Ok, I managed to get the custom validator to only get called once. I removed the line "OnServerValidate="ValidateFirstName_ServerValidate">"
that seems to have fixed the problem.

If anyone could still tell me why the regular expression validator always displayed the error message not matter if the reg ex matached or didn't I'd appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top