javascript 如何在客户端设置自定义验证器 isValid 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17251551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:38:49  来源:igfitidea点击:

How to set custom validator isValid property on Client Side?

javascriptasp.net

提问by SJMan

I am having a problem setting the customvalidator on client side. based on a particular hidden field value, the onClientClick event of a button should be firing a function which sets the isValid property of a CustomValidator to false.

我在客户端设置 customvalidator 时遇到问题。基于特定的隐藏字段值,按钮的 onClientClick 事件应该触发一个函数,该函数将 CustomValidator 的 isValid 属性设置为 false。

Below is some code : Code Behind :

下面是一些代码: 背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (hiddenfieldValue == true)
    {
        btnSend.OnClick = "someJavascriptFunction()";
    }
}

ASPX File :

ASPX 文件:

function someJavascriptFunction()
{
    if (hiddenfieldValue == true)
      // Show Validation and dont do postback
      vldValdiator.isValid = false; 
      return false;   //Dont do Postback
    else
      return true; Do Postback
}

<asp:CustomValidator ID="vldValidator" runat="server" Text = "ABC"/>

I am not able to set the isValid property on client side based hidden field values. Please Help . Thanks in advance.

我无法在基于客户端的隐藏字段值上设置 isValid 属性。请帮忙 。提前致谢。

回答by Crab Bucket

Method 1

方法一

You could try setting the Page_IsValidproperty to false within the button onClientClick event. This would force the page into it's non validate state as far as the asp.net validators are concerned.

您可以尝试Page_IsValid在 onClientClick 事件按钮中将该属性设置为 false。就asp.net 验证器而言,这将强制页面进入非验证状态。

Basic example

基本示例

function ButtonOnClickClient()
{
   if ($('#hiddenFieldID').val() == 'someValue')
   {
      Page_IsValid = false;
   }
}

Honestly - i'm not convinced that the Page_IsValid property won't be reset. You would need to try

老实说 - 我不相信 Page_IsValid 属性不会被重置。你需要尝试

Method 2

方法二

you could make the customvalidator client validation check the hidden field and make the validator pass or fail validation on that basis. The button onclient click event could then call the Page_ClientValidate()function which will force validation on the page. This will then set the correct validation property.

您可以让 customvalidator 客户端验证检查隐藏字段,并在此基础上让验证器通过或失败验证。然后,客户端单击事件上的按钮可以调用Page_ClientValidate()将强制在页面上进行验证的函数。这将设置正确的验证属性。

Basic example 2

基本示例 2

function CustomValidatorClientValidate(source, arguments)
   {
        if ($('#hiddenFieldID').val() == 'someValue'){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }

function ButtonOnClientClick()
{
    Page_ClientValidate();
}

This uses JQuery - because i find it easier to write an example that way. By no means mandatory to use it. Also I'm assuming the asp.net validator API is present. It should be if you have included those controls but you could always tighten up by adding checks for the function names

这使用了 JQuery - 因为我发现以这种方式编写示例更容易。绝不强制使用它。此外,我假设存在 asp.net 验证器 API。如果你已经包含了这些控件,但你总是可以通过添加对函数名称的检查来收紧

ALSO

If you are using validation groups which you probably will be the call will become

如果您正在使用验证组,您可能会被调用

Page_ClientValidate(validationGroupName);

Method 3

方法三

You could try that about but set the the isvalid property of the customvalidator. That should give you the more direct control of the validator that you want. The reference below says

您可以尝试一下,但设置 customvalidator 的 isvalid 属性。这应该可以让您更直接地控制所需的验证器。下面的参考说

isvalid Boolean property.This is a property on each client validator indicating whether it is currently valid

isvalid 布尔属性。这是每个客户端验证器上的一个属性,指示它当前是否有效

I've done something similar to the first two but I've not personally tried this one.

我做过与前两个类似的事情,但我没有亲自尝试过这个。

In general

一般来说

The general the method you are using is direct manipulation of the javascript API that the asp.net validators use. It's not massively pretty but you can get the control that you want

您使用的一般方法是直接操作 asp.net 验证器使用的 javascript API。它不是非常漂亮,但你可以得到你想要的控制

Reference

参考

This gives a reference to the javascript API you are trying to use. It's a lengthy article but the bit you want is about halfway down

这提供了对您尝试使用的 javascript API 的引用。这是一篇冗长的文章,但您想要的部分大约是一半

http://msdn.microsoft.com/en-us/library/Aa479045

http://msdn.microsoft.com/en-us/library/Aa479045

Good Luck

祝你好运

More

更多的

I really need to leave this but your code is wrong here - it's OnClientClick.

我真的需要离开这个,但你的代码在这里是错误的 - 它是 OnClientClick。

 btnSend.OnClientClick = "someJavascriptFunction()";

回答by Raghubar

Use This to Bind the Event.

使用它来绑定事件。

btnSend.Attributes.Add("onclick","return someJavascriptFunction();")

btnSend.Attributes.Add("onclick","return someJavascriptFunction();")