0

We know usually the CustomValidator control will check the user's input against some arithmetic validation.

I have a textbox in my web form. The text of it doesn't come from user's input, it comes from the database.

MembershipUser user = Membership.Providers["SqlMembershipProvider"].GetUser(userName);
TextUserName.Text = AntiXss.HtmlEncode(user.UserName);

My goal is to use some kind of validator to check whether it is appropriate. If not then change it in the textbox and validate it again. How to do it?

Thanks.

UPDATE CODE:

    protected void ValidateUser()
    {
        string UserNameCreated = TextUserName.Text;
        Match match = Regex.Match(UserNameCreated, @"^[a-zA-Z0-9]{6,}$",
    RegexOptions.IgnoreCase);
    }

     <td class="style4">
            <asp:TextBox ID="TextUserName" runat="server"></asp:TextBox>
        </td><td><asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                                ErrorMessage="Minimum of 6 (six) alphanumeric characters." 
                OnServerValidate="ValidateUser" Display="Dynamic"
                                ValidateEmptyText="True" ></asp:CustomValidator></td>
  • 1
    Validator's purpose is just to validate user input. You might want to use some kind of RegularExpression. You should validate the input **before** you store it in the database. – Tim Schmelter Apr 18 '12 at 13:13
  • You can do it in this pattern. But my case needs me to check it after its displaying the text. –  Apr 18 '12 at 13:52
  • You haven't shown your CustomValidator. What problems do you actually have, what is the question? If you set `TextUserName.Text` to the current user's `UserName` and he's able to change it, what's the problem then to provide a Validator that validates that input? – Tim Schmelter Apr 18 '12 at 13:58
  • The program never reachs the code for validating the textbox. Code updated. –  Apr 18 '12 at 14:07

1 Answers1

0

Your ServerValidate has the wrong signature.

void ServerValidation(object source, ServerValidateEventArgs args)
{
    args.IsValid = Regex.Match(TextUserName.Text, @"^[a-zA-Z0-9]{6,}$", RegexOptions.IgnoreCase);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • No I just changed it for demo purpose. Originally it was same as your code. But it never reached it. Maybe it is not user' input? –  Apr 18 '12 at 14:16
  • Let me clarify it. I meant that the code did go to validator part, but I found args.IsValid = false; Then it seems the validator is not working. It is supposed to display the error message and stuck on the textbox. But it is not. –  Apr 18 '12 at 14:25
  • @Love: Have a look at a recent answer on a similar question. It might be helpful: http://stackoverflow.com/a/10172624/284240 Is the code above complete or just a snippet? Maybe you have other validators that prevent the ServerValidate from executing. – Tim Schmelter Apr 18 '12 at 14:25
  • I have a button for submitting new change. Do I have to add Validate Group? –  Apr 18 '12 at 14:33
  • 1
    Not necessarily, if you don't specify a group the whole page will be validated. Does the page post back when you click the button? You could force validation in the button's click-handler by `Page.Validate()`(as shown in the link above). – Tim Schmelter Apr 18 '12 at 14:36
  • @Love: In general it would be better to place it in an event handler(button-click) since you want to validate user input. – Tim Schmelter Apr 18 '12 at 15:05