Click here to Skip to main content
15,880,891 members

Asp.net Validators not working with Client side javascript

Member 9581909 asked:

Open original thread
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>
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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