简体   繁体   English

是否可以在 asp.net 控件上进行仅客户端验证?

[英]Is it possible to have client-side only validation on asp.net controls?

First, I know I can use Javascript, but let me explain.首先,我知道我可以使用 Javascript,但让我解释一下。

My goal is to be able to use any of the validators:我的目标是能够使用任何验证器:

  • Required必需的
  • Regex正则表达式
  • Compare比较
  • etc.等等

And benefit only from their client-side validation.并且只从他们的客户端验证中受益。 I'm sub-classing inherited controls and preventing the name attribute from being rendered to the client.我正在对继承的控件进行子类化,并防止将名称属性呈现给客户端。 The reason is for sensitive data that I don't want going back to the server.原因是我不想返回服务器的敏感数据。 Removing the name attribute prevents the value from being sent with the control.删除名称属性可防止值随控件一起发送。

Take required field for example, it always fails validation on the server because the value isn't sent.以必填字段为例,它总是在服务器上验证失败,因为未发送值。 I could use a custom validator but want to leverage all the pre-built features of all the validate types.我可以使用自定义验证器,但想利用所有验证类型的所有预构建功能。

Hoping someone had a creative idea, thought, or if I'm blind to the obvious, or if it's just straight up impossible.希望有人有创意,想法,或者如果我对显而易见的东西视而不见,或者如果它是不可能的。

Resetting the IsValid property of the validator in the overridden Validate function of the page seems to work:在页面的重写Validate函数中重置验证器的IsValid属性似乎有效:

public override void Validate()
{
    base.Validate();
    rfvNotEmpty.IsValid = true;
}

I know this question is pretty only but if someone need it, I've got a simple solution.我知道这个问题很简单,但如果有人需要,我有一个简单的解决方案。

You simply created your client-side validator to be Enabled="false" and in the PreRender event you enabled It, The server-side validation appear before the PreRender so it will be ignore because it's disabled, but the prerender will generate client-side code and the validator will be enabled and working.您只需将客户端验证器创建为 Enabled="false" 并在 PreRender 事件中启用它,服务器端验证出现在 PreRender 之前,因此它将被忽略,因为它被禁用,但预渲染将生成客户端代码和验证器将被启用并工作。

You can try to use ASP.Net Ajax Control Toolkit, it is an open source library for web development.您可以尝试使用 ASP.Net Ajax Control Toolkit,它是一个用于 Web 开发的开源库。

If you want to use standard Asp.Net Control, you can use the validation controller there is compare, custom, range, regex, and required field validator.如果您想使用标准的 Asp.Net 控件,您可以使用验证控制器,其中包含比较、自定义、范围、正则表达式和必填字段验证器。

regards问候

If i understand you question correctly you want your server side validation control to work client side, then you need to add reference of latest jquery file on your page as showing in example.如果我理解你的问题是正确的,你希望你的服务器端验证控件在客户端工作,那么你需要在你的页面上添加对最新 jquery 文件的引用,如示例所示。

Server side control will validated client side and form will not be posted to server side until validated client side.服务器端控件将验证客户端,并且在验证客户端之前不会将表单发布到服务器端。

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ValidationTest.aspx.vb" Inherits=".ValidationTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
    <asp:Panel runat="server">
        <asp:TextBox runat="server" ID="txtName" CssClass="form-control"  />
        <asp:RequiredFieldValidator
            ID="rvtxtName"
            InitialValue=""
            runat="server"
            ControlToValidate="txtName"
            ForeColor="Red"
            ErrorMessage="*Field Required!"
            Display="Dynamic"
            ValidationGroup="add">
        </asp:RequiredFieldValidator>
        <asp:Button runat="server" ID="btnSubmit" Text="Save"  ValidationGroup="add" />
    </asp:Panel>


   </form>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM