Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey friends,

I have two Asp.net validators on my textboxes.
The TxtPassword has 2 validators:

1.Asp.Net RequiredField Validator.
2.Password length implemented by me with JavaScript.

both are bound to button.


My problem is that...

Whenever i enter the password greater than 20 characters..everything works fine.
But when i leave the TxtPassword empty,the Client side script is bypassed.Sure this is correct.But my Required field validator doesnt work and control flows to server side.and i redirect to other page.





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginPage.aspx.cs" Inherits="LoginPage" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Migration Issue Tracker</title>
    <script type="text/javascript">
    //<![CDATA[
    function checkforlength() {
        var element = document.getElementById("TxtPassword").value;
        if (element.length > 20) {
            alert("password exceeded the range");
            return false;
        }
        else {return true;}
        }
//
</script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    
        <asp:Panel ID="LoginPanel" runat="server" Width="500px" Height="650px" 
        BorderColor="#00CC66" BorderStyle="Inset" BackColor="ActiveCaption"
        style="position: relative; top: 5px; left: 1065px">
        <asp:Label ID="Label1" runat="server" Text="User Name" 
            style="z-index: 1; left: 58px; top: 170px; position: absolute" 
            Font-Size="X-Large" Font-Bold="True"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Password" 
            style="z-index: 1; left: 60px; top: 221px; position: absolute" 
            Font-Size="X-Large" Font-Bold="True"></asp:Label>
        <asp:TextBox ID="TxtUserName" runat="server"   ValidationGroup="ValGroup1"        
            
            style="position: absolute; z-index: 1; left: 193px; top: 171px; height: 21px; width: 197px;" 
            TabIndex="1" EnableViewState="False" CausesValidation="True" 
           ></asp:TextBox>
        
        <asp:Label ID="Label6" runat="server" Text="*" Font-Bold="True" 
            Font-Size="Medium" ForeColor="#CC3300" 
            style="z-index: 1; left: 49px; top: 166px; position: absolute"></asp:Label>
        <asp:Label ID="Label7" runat="server" Text="*" ForeColor="#CC3300" 
            style="z-index: 1; left: 51px; top: 219px; position: absolute"></asp:Label>
        <asp:RequiredFieldValidator ID="RequiredFieldValidatorUserName" runat="server" 
            ErrorMessage="The fields marked with asterisk(*) are mandatory" ControlToValidate="TxtUserName" 
            style="z-index: 1; left: 136px; top: 131px; position: absolute" 
            ValidationGroup="ValGroup1"></asp:RequiredFieldValidator>
            
        <asp:Button ID="Button1"
            runat="server" Text="Log In" 
            style="z-index: 2; left: 194px; top: 290px; position: absolute" OnClientClick="return checkforlength();"
            onclick="Button1_Click" ValidationGroup="ValGroup1" 
            CausesValidation="True" />
        <asp:HyperLink ID="HyperLink2" runat="server"  
            style="z-index: 1; left: 194px; top: 383px; position: absolute" 
            TabIndex="4" Font-Names="Arial" Font-Size="Large" 
            NavigateUrl="~/ForgotPwd.aspx">Forgot Password?</asp:HyperLink>
            
        <asp:HyperLink ID="HyperLink1" runat="server" Font-Bold="True" 
            Font-Names="Arial" Font-Size="Large" Font-Underline="True" 
            style="z-index: 1; left: 295px; top: 52px; position: absolute" 
            NavigateUrl="~/CreateYourAccount.aspx">Create your Account</asp:HyperLink>
        
        
        <asp:TextBox ID="TxtPassword" runat="server" ValidationGroup="ValGroup1"
            
            style="z-index: 1; left: 192px; top: 223px; position: absolute; height: 21px; width: 195px;" 
            TabIndex="2" EnableViewState="False" TextMode="Password"
            ToolTip="This Field is case sensitive." CausesValidation="True"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
          ErrorMessage="The fields marked with asterisk(*) are mandatory" 
            ControlToValidate="TxtPassword" ValidationGroup="ValGroup1" 
            style="z-index: 1; left: 136px; top: 131px; position: absolute"></asp:RequiredFieldValidator>
            

        
        
      
</body>
</html>
Posted

use regular expression intead of javascript,
 
Share this answer
 
Add another validator to the text box

XML
<asp:CustomValidator runat="server"
        ID="CustomValidator"
        ControlToValidate="TxtPassword"
        ClientValidationFunction="checkforlength"
        ValidateEmptyText="true"
        Text="Error!">
    </asp:CustomValidator>



Also update your javascript function

C#
function checkforlength(sender, args) {
    if (args.Value.length > 20) {
        alert("password exceeded the range");
        return false;
    }
    else {return true;}
    }
 
Share this answer
 

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