Kings
Kings

Reputation: 1591

Client side validation for a password textbox value

I have a web form to submit a new user details. In that for password field, a textbox is used. Validation is required for that as 'it shold contain min 8 characters with atleast one non-alphanumeric character'. I'm providing validation for it in web config file as:-

<membership defaultProvider="SQLMembershipProvider">
  <providers>
      <remove name="SQLMembershipProvider"/>
        <add name="SQLMembershipProvider" connectionStringName="DAFEConnection" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="1"   type="System.Web.Security.SqlMembershipProvider" applicationName="WBCPDA" requiresQuestionAndAnswer="false" enablePasswordReset="true" maxInvalidPasswordAttempts="4" passwordAttemptWindow="15" requiresUniqueEmail="true"/>
            </providers>
        </membership>

This validation is triggered on server side, i.e. when I click 'submit' button. I WANT TO VALIDATE THE SAME AT CLIENT-SIDE, i.e. as soon as he enters a non-desired value in text-box.

PLZ help

Upvotes: 0

Views: 3337

Answers (2)

Icarus
Icarus

Reputation: 63966

The quickest way to do this would be using a Regular Expression Validator. See documentation here.

Kangkan's suggestion is very good; not only the idea to use jQuery for this; but mostly the idea of using a regular expression to validate that the password is in the expected format.

Additional answer:

It's difficult The quickest way to give you exact code that will work for your case since you didn't provide markup; but you could do something similar to this :

<asp:button id="btnSubmit" runat="server" OnClientClick="return validate_pass();" OnClick="YourHandler" />

And would be using a javascript function .Regular Expression Validator. See documentation here.

function validate_pass()
{
    //txtPassword is assumed to be the id of the password text field on your page
    var passField = document.getElementById('<%=txtPassword.ClientID%>');
    if(passField.value.length<8)
    {  
       alert('Password must contain at least 8 characters');
       return false; 
    }
    return true;
}

Upvotes: 2

Kangkan
Kangkan

Reputation: 15571

You can use javascript or jQuery plugins to check for the password rules in the client side. You can refer to http://plugins.jquery.com/plugin-tags/password-validation or http://bassistance.de/jquery-plugins/jquery-plugin-password-validation/. You may even roll out your own javascript code and use RegEx to validate.

Upvotes: 1

Related Questions