Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"   OnTextChanged="javascript:validatePwd();" CausesValidation="True"  AutoPostBack="true" MaxLength="12">


in this code i wanted to call "validatePwd()" which is defined as below...

C#
<script type="text/javascript" language="javascript">

    function validatePwd()
     {
        var invalid = " "; // Invalid character is a space
        var minLength = 6; // Minimum length

        var pw1 = document.getElementById('txtPwd');
        var pw2 = document.getElementById('txtRePwd');

        // check for a value in both fields.

        if (pw1 == '' || pw2 == '') {
            alert('Please enter your password twice.');
            return false;
        }
        // check for minimum length
        if (document.getElementById('txtpwd'.length()) < minLength) {
            alert('Your password must be at least ' + minLength + ' characters long. Try again.');
            return false;
        }
        // check for spaces
        if (document.getElementById('txtpwd'.indexOf(invalid)) > -1) {
            alert("Sorry, spaces are not allowed.");
            return false;
        }
        else {
            if (pw1 != pw2) {
                alert("You did not enter the same new password twice. Please re-enter your password.");
                return false;
            }
            else {
                alert('PASSWORD MATHCHED');
                return true;
            }
        }
    }

</script>
Posted
Updated 8-Feb-12 22:28pm
v2

Try this, i have changed your JavaScript
XML
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"  MaxLength="12">

</asp:TextBox>
<asp:TextBox ID="txtRePwd" runat="server" TextMode="Password"  MaxLength="12">

</asp:TextBox>
<input type='button' onclick="return validatePwd();" />

<script type="text/javascript" language="javascript">
    function validatePwd() {
        var invalid = " "; // Invalid character is a space
        var minLength = 6; // Minimum length

        var pw1 = document.getElementById('<%=txtPwd.ClientID%>').value;
        var pw2 = document.getElementById('<%=txtRePwd.ClientID%>').value;

        // check for a value in both fields.

        if (trim(pw1) == '' || trim(pw2) == '') {
            alert('Please enter your password twice.');
            return false;
        }
        else
        // check for minimum length
        if (pw1.length < minLength) {
            alert('Your password must be at least ' + minLength + ' characters long. Try again.');
            return false;
        }
        // check for spaces
                else {
                    if (pw1.toUpperCase() != pw2.toUpperCase()) {
                alert("You did not enter the same new password twice. Please re-enter your password.");
                return false;
            }
            else {
                alert('PASSWORD MATHCHED');
                return true;
            }
        }
    }
    //function to trim the text
    function trim(str) { return str.replace(/^\s*|\s*$/g, ""); }
</script>
 
Share this answer
 
Refer this
http://blog.stevenlevithan.com/archives/javascript-password-validator[^]
Your code contains lots of error
1. You are not using trim
2. Length is not used like this
3. While comparing password, you are not converting then to same case either lower or upper.
4. If you are using runat server and your page contains a master page, then use ClientID to get the textbox values.
 
Share this answer
 
Hi,

I think you should have a submit button to click, where upon clicking such button should call your function for validation...
as example:
C#
 <asp:button id="btnLogin" runat="server" text="Login" xmlns:asp="#unknown">
    OnClientClick= "return validatePwd();" style="margin-left: 0px" />
</asp:button>


Regards,
 
Share this answer
 
Try this

C#
<script type="text/javascript" language="javascript">

     function validatePwd() {
         var invalid = " "; // Invalid character is a space
         var minLength = 6; // Minimum length
        
         var pw1 = document.getElementById('<%=txtPwd.ClientID%>').value;
         var pw2 = document.getElementById('txtRePwd');

         // check for a value in both fields.
       
         if (pw1 == '' || pw2 == '') {
             alert('Please enter your password twice.');
             return false;
         }
        
         if (document.getElementById('<%=txtPwd.ClientID%>'.length) < minLength) {
             alert('Your password must be at least ' + minLength + ' characters long. Try again.');
             return false;
         }
         // check for spaces
         if (document.getElementById('<%=txtPwd.ClientID%>'.indexOf(invalid)) > -1) {
             alert("Sorry, spaces are not allowed.");
             return false;
         }
         else {
             if (pw1 != pw2) {
                 alert("You did not enter the same new password twice. Please re-enter your password.");
                 return false;
             }
             else {
                 alert('PASSWORD MATHCHED');
                 return true;
             }
         }
     }
 
</script>

    <asp:textbox id="txtPwd" runat="server" textmode="Password" onchange="return validatePwd();" causesvalidation="True" maxlength="12" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 10-Feb-12 5:16am    
Added pre tag
Sanjay K. Gupta 10-Feb-12 5:35am    
Hi shweta9999,
Please accept the answer. A P Indore have given you right code.
Since you want to do client side validation using javascript you do not need CausesValidation = "True" and AutoPostBack="True". These are server side validation property.
Also you have use OnTextChanged="javascript:validatePwd();". This is server side event which will cause postback.

You can modify your code as

C#
<asp:textbox id="txtPwd" runat="server" textmode="Password" onblur="return validatePwd();" maxlength="12" xmlns:asp="#unknown">


or
C#
<asp:textbox id="txtPwd" runat="server" textmode="Password" onchange="return validatePwd();" maxlength="12">


using onblur will cause validation when textbox leave focus and
using onchange will cause validation when textbox value is changed.

Also you can make your javascript validation simple by doing most validation with txtPwd textbox onblur or onchange. And just do comparison validation "You did not enter the same new password twice. Please re-enter your password." onchange of txtRePwd textbox
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900