Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 3-Feb-13 20:16pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use regular expression intead of javascript,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add another validator to the text box
 
<asp:CustomValidator runat="server"
        ID="CustomValidator"
        ControlToValidate="TxtPassword"
        ClientValidationFunction="checkforlength"
        ValidateEmptyText="true"
        Text="Error!">
    </asp:CustomValidator>
 

Also update your javascript function
 
function checkforlength(sender, args) {
    if (args.Value.length > 20) {
        alert("password exceeded the range");
        return false;
    }
    else {return true;}
    }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 398
1 Richard Deeming 210
2 Sergey Alexandrovich Kryukov 202
3 PhilLenoir 120
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100