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:
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:
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.
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.