Skip to main content
 首页 » 编程设计

c#之.net 验证

2024年02月23日18kevingrace

我在 .net 中构建的网站上工作,但在“验证”方面遇到了一些麻烦

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"  
    ErrorMessage="<p>Invalid Phone Number!</p>"........ 

这是用内联样式在页面上发布一条消息,我需要它做的是向输入字段添加一个类,这可能吗?

请您参考如下方法:

我建议为此使用 CustomValidator:

http://msdn.microsoft.com/en-us/library/9eee01cx(v=VS.100).aspx

<asp:CustomValidator id="CustomValidator1" 
       ControlToValidate="Text1" 
       ClientValidationFunction="ClientValidate" 
       ErrorMessage="<p>Invalid Phone Number!</p>" 
       runat="server"/> 
 
 
<script language="javascript"> 
   function ClientValidate(source, arguments) 
   { 
      var regexValid = false; // perform regular expression validation here manually 
      if (regexValid) { 
         arguments.IsValid=true; 
      } 
      else { 
         // add the class to the desired input field 
         arguments.IsValid=false; 
      } 
   } 
</script>