Asp.net Validators不使用客户端javascript [英] Asp.net Validators not working with Client side javascript

查看:57
本文介绍了Asp.net Validators不使用客户端javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友,



我的文本框上有两个Asp.net验证器。

TxtPassword有2个验证器:



1.Asp.Net RequiredField Validator。

2.使用JavaScript实现的密码长度。



都必须按钮。





我的问题是......



每当我输入大于20个字符的密码时..一切正常。

但是当我将TxtPassword保留为空时,会绕过客户端脚本。这是正确的。但我的必填字段验证器不起作用,控制流到服务器端。我重定向到其他页面。











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>

推荐答案

使用javascript的正则表达式intead,
use regular expression intead of javascript,


在文本框中添加另一个验证器



Add another validator to the text box

<asp:CustomValidator runat="server"

        ID="CustomValidator"

        ControlToValidate="TxtPassword"

        ClientValidationFunction="checkforlength"

        ValidateEmptyText="true"

        Text="Error!">
    </asp:CustomValidator>







还要更新你的javascript函数






Also update your javascript function

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


这篇关于Asp.net Validators不使用客户端javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆