ASP.net的CustomValidator - ClientValidationFunction检查值不为空,而不是初始值 [英] ASP.net CustomValidator - ClientValidationFunction to check value not blank and not initial value

查看:159
本文介绍了ASP.net的CustomValidator - ClientValidationFunction检查值不为空,而不是初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code这也验证了一个文本框的值,以确保它不为空,但我还需要检查它不等于文本框(设置defaultValue)的初始值。

这是我迄今为止...

的Javascript:

 函数textValidation(来源参数)
   {
        如果(arguments.Value =!){//&放大器;&放大器; arguments.Value!= arguments.defaultValue
            arguments.IsValid = TRUE;
        }其他{
            $(源)。家长(格),CSS({背景颜色:红色});
            arguments.IsValid = FALSE;
        }
   }

.NET

 < ASP:文本框=服务器ID =初始文本=初始设置defaultValue =初始WIDTH =120像素/>< ASP:的CustomValidator ID =Initial_req
     的ControlToValidate =初始
     ClientValidationFunction =textValidation
     ValidateEmptyText =真
     =服务器
     的CssClass =errorAsterisk
     文本=*
     的ErrorMessage =完成所有通信领域的/>


解决方案

您可以做你想要用什么CSS类,以确定文本框,并使用jQuery检索它,可以让您获得属性设置defaultValue

标记:

 < ASP:文本框=服务器
             ID =初始
             文本=初始
             设置defaultValue =初始
             WIDTH =120像素
             的ValidationGroup =测试
             的CssClass =到验证/>< ASP:的CustomValidator ID =Initial_req
   的ControlToValidate =初始
   ClientValidationFunction =textValidation
   ValidateEmptyText =真
   =服务器
   的CssClass =errorAsterisk
   文本=*
   的ErrorMessage =完成所有通信领域
   的ValidationGroup =测试/>< ASP:按钮的ID =btnValidate=服务器文本=验证的ValidationGroup =测试/>

使用Javascript:

 函数textValidation(来源参数){
     VAR初值= $(源).siblings(对验证:先)。ATTR(设置defaultValue);     如果(arguments.Value =与&!&安培; arguments.Value =初值!){//&放大器;&安培; arguments.Value!= arguments.defaultValue
         arguments.IsValid = TRUE;
     }其他{
         $(源)。家长(格),CSS({背景颜色:红色});
         arguments.IsValid = FALSE;
     }
 }

I have following code which validates the value of a textbox to make sure it's not blank but I also need to check that it does not equal the initial value of the textbox (defaultValue).

Here's what I have so far...

Javascript:

function textValidation(source, arguments)
   {
        if ( arguments.Value != "" ){ // && arguments.Value != arguments.defaultValue
            arguments.IsValid = true;
        } else {
            $(source).parents("div").css({"background-color":"red"});
            arguments.IsValid = false;
        }
   }

.net

  <asp:TextBox runat="server" ID="Initial" Text="Initial" defaultValue="Initial" Width="120px" />                               

<asp:CustomValidator id="Initial_req"
     ControlToValidate="Initial"
     ClientValidationFunction="textValidation"
     ValidateEmptyText="true"
     runat="server"
     CssClass="errorAsterisk"
     Text="*" 
     ErrorMessage="Complete all correspondence fields" />

解决方案

You can do what you want using a CSS class to identify the TextBox and retrieve it with jQuery, allowing you to obtain the attribute defaultValue:

Markup:

<asp:TextBox runat="server" 
             ID="Initial" 
             Text="Initial" 
             defaultValue="Initial" 
             Width="120px" 
             ValidationGroup="Test" 
             CssClass="to-validate" />

<asp:CustomValidator ID="Initial_req" 
   ControlToValidate="Initial" 
   ClientValidationFunction="textValidation"
   ValidateEmptyText="true" 
   runat="server" 
   CssClass="errorAsterisk" 
   Text="*" 
   ErrorMessage="Complete all correspondence fields"
   ValidationGroup="Test" />

<asp:Button ID="btnValidate" runat="server" Text="Validate" ValidationGroup="Test" />

Javascript:

function textValidation(source, arguments) {
     var initialValue = $(source).siblings(".to-validate:first").attr("defaultValue");

     if (arguments.Value != "" && arguments.Value != initialValue) { // && arguments.Value != arguments.defaultValue
         arguments.IsValid = true;
     } else {
         $(source).parents("div").css({ "background-color": "red" });
         arguments.IsValid = false;
     }
 }

这篇关于ASP.net的CustomValidator - ClientValidationFunction检查值不为空,而不是初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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