samedi 5 novembre 2016

C# Validating a password on SERVER SIDE

I'm right now trying to validate a password on the server side using .NET

Here's my client side code:

                    <asp:TextBox ID="PSWRD" runat="server" Width="120px"></asp:TextBox>
                    <span style="background-color:yellow; color:red; font-weight:bold;" title="some rules">Password Rules</span>

                     <asp:RequiredFieldValidator ID="VPSWRD" runat="server" 
                        ErrorMessage="A password is required"
                        ControlToValidate="PSWRD">
                        </asp:RequiredFieldValidator>

                    <asp:CustomValidator ID="IPSWRDVal" runat="server"
                        OnServerValidate="ValidatePasswordServerSide"
                        ControlToValidate="PSWRD"
                        ErrorMessage="Please enter a *valid* password"
                        ValidateEmptyText="False"
                        ></asp:CustomValidator>

My first validator works fine. It displays it's message when appropriate.

The second one gives me trouble.

Here's my code behind

protected void ValidatePasswordServerSide(object source, ServerValidateEventArgs args)
{


        if (Regex.IsMatch(args.Value, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
            args.IsValid = true;
        else
            args.IsValid = false;


}

The issue is that I'm not seeing an error message display for my second validator whenever I enter an incorrect value.

Also a little plea, before people get too exotic with their solutions please note this for a school assignment, so they want a cookie cutter approach. I know, it's dumb but rules are rules.




Aucun commentaire:

Enregistrer un commentaire