如何通过javascript更改CompareValidator中的ValueToCompare?

时间:2021-01-09 15:52:29

In my form, there are password field, so that I cannot use postback. I need to validate everything in client side.

在我的表单中,有密码字段,所以我不能使用回发。我需要验证客户端的所有内容。

But the ValueToCompare may change by the user input.

但ValueToCompare可能会因用户输入而改变。

How to change the ValueToCompare in CompareValidator by javascript?

如何通过javascript更改CompareValidator中的ValueToCompare?

Thanks.


Maybe I need to tell the full case. I have two input, said a & b, the sum of this two number cannot greater than c.

也许我需要告诉完整案例。我有两个输入,说a&b,这两个数的总和不能大于c。

For example, c is 90 a is inputted 30, then b cannot be greater than 60. if b is then entered to 40, than a cannot be greater than 50.

例如,c是90a输入30,则b不能大于60.如果b然后输入40,则a不能大于50。

I am now doing this in server side, how can I do it in client side by using asp.net validations?

我现在在服务器端这样做,如何通过使用asp.net验证在客户端执行此操作?

4 个解决方案

#1


This works for me:

这对我有用:

<asp:CompareValidator ID="valOptionsCmp" runat="server"
     ErrorMessage="!!!"
     ControlToValidate="tbPrixOptions" Display="Dynamic"
     ValueToCompare="1"
     Operator="GreaterThanEqual"
     Type="Integer"/>

//in javascript
<%=valOptionsCmp.ClientID%>.valuetocompare = yourvalue

#2


From:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx

Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.

将输入控件中用户输入的值与另一个输入控件中输入的值或常量值进行比较。

Those are your choices. Compare two controls, or compare one against a constant.

这些是你的选择。比较两个控件,或将一个控件与常量进行比较。

If neither suits, you may need to roll your own.

如果两者都不合适,你可能需要自己动手。

#3


If you means something like Enter Your Password and Please Reenter Password type of comparison then you don't need to use ValueToCompare. Instead you use ControlToCompare and ControlToValidate like this:

如果您的意思是输入您的密码和请重新输入密码类型的比较,那么您不需要使用ValueToCompare。而是像这样使用ControlToCompare和ControlToValidate:

  <asp:TextBox id="password" runat="server" TextMode="password"/>
  <asp:RequiredFieldValidator id="rfvPass" runat="server" 
   ControlToValidate="password" Text="*"/>

  <asp:textbox id=="rePassword" runat="server" TextMode="Password"/>
  <asp:CompareValidator id="cmpPass" runat="server" ControlToCompare="password" 
   ControlToValidate="rePassword" Type="String" Operator="Equal"/>

#4


I solved the same problems with firebug helps:

我用firebug帮助解决了同样的问题:

<script type="text/javascript">
        $(document).ready(function() {
            $("select[id$=ddlYears]").change(function() {
              var year= $(this).val(); 

              <%= cvDataMinApertura.ClientID%>.valuetocompare = "01/01/"+year;          

            });
        });
    </script> 

I use JQuery for change compare validator cvDataMinApertura on change of years drop down list.

我在更改年份下拉列表时使用JQuery进行更改比较验证器cvDataMinApertura。

#1


This works for me:

这对我有用:

<asp:CompareValidator ID="valOptionsCmp" runat="server"
     ErrorMessage="!!!"
     ControlToValidate="tbPrixOptions" Display="Dynamic"
     ValueToCompare="1"
     Operator="GreaterThanEqual"
     Type="Integer"/>

//in javascript
<%=valOptionsCmp.ClientID%>.valuetocompare = yourvalue

#2


From:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx

Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.

将输入控件中用户输入的值与另一个输入控件中输入的值或常量值进行比较。

Those are your choices. Compare two controls, or compare one against a constant.

这些是你的选择。比较两个控件,或将一个控件与常量进行比较。

If neither suits, you may need to roll your own.

如果两者都不合适,你可能需要自己动手。

#3


If you means something like Enter Your Password and Please Reenter Password type of comparison then you don't need to use ValueToCompare. Instead you use ControlToCompare and ControlToValidate like this:

如果您的意思是输入您的密码和请重新输入密码类型的比较,那么您不需要使用ValueToCompare。而是像这样使用ControlToCompare和ControlToValidate:

  <asp:TextBox id="password" runat="server" TextMode="password"/>
  <asp:RequiredFieldValidator id="rfvPass" runat="server" 
   ControlToValidate="password" Text="*"/>

  <asp:textbox id=="rePassword" runat="server" TextMode="Password"/>
  <asp:CompareValidator id="cmpPass" runat="server" ControlToCompare="password" 
   ControlToValidate="rePassword" Type="String" Operator="Equal"/>

#4


I solved the same problems with firebug helps:

我用firebug帮助解决了同样的问题:

<script type="text/javascript">
        $(document).ready(function() {
            $("select[id$=ddlYears]").change(function() {
              var year= $(this).val(); 

              <%= cvDataMinApertura.ClientID%>.valuetocompare = "01/01/"+year;          

            });
        });
    </script> 

I use JQuery for change compare validator cvDataMinApertura on change of years drop down list.

我在更改年份下拉列表时使用JQuery进行更改比较验证器cvDataMinApertura。