2011-11-14 50 views
0

我使用.net驗證控件,並且我能夠對除了獲取驗證的控件以外放置的錯誤文本進行樣式設置(在我們的示例中,我們顯示一個紅色星號)。使用.net驗證控件時的樣式驗證控件

所以我可以風格的紅色星號,但在這種情況下,我也想樣式文本框/輸入其中的誤差是英寸

能以某種方式與標準驗證控件做到這一點?

基本上我想要的是'詢問'所有的驗證器,以驗證它們控制的控件和樣式(即向它應用一個css類)。

米歇爾

+0

一個ValidatorCalloutExtender這可以通過默認:http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ValidatorCallout/ValidatorCallout。 aspx('HighlightCssClass') –

+0

檢查這篇文章:http://www.delphicsage.com/home/blog.aspx?d=545 –

回答

1

你應該能夠用CustomValidator做到這一點。這裏有一個簡單的例子:

<script type="text/javascript"> 
    validateStuff = function(sender, args){ 
     args.IsValid = false; //your validaiton logic 
     if (!args.IsValid){ 
      var el = document.getElementById(sender.controltovalidate); 
      if (el){ 
       el.style.border = "1px solid red"; 
      } 
     }    
    } 
</script> 
<asp:TextBox ID="TextBox1" runat="server" /> 
<asp:CustomValidator runat="server" id="CustomValidator1" 
     ControlToValidate="TextBox1" 
     ClientValidateFunction="validateStuff" 
     ErrorMessage="Invalid Prime Number"> 
</asp:CustomValidator> 

你也可以使用jQuery Validation Plugin。我沒有測試過這一點,但這裏有一個如何hightlight元件的一例:

$("#<%=form1.ClientID%>").validate({ 
    rules: { 
     <%= TextBox1.ClientID %> : { 
      required: true     
     } 
    },  
    highlight: function(element, errorClass) {   
     $(element).css({ border: "1px solid red", color : "red" }); 
    } 
});