在ASP.NET Atlas中创建自定义的Validator

Enlish Version: http://dflying.dflying.net/1/archive/112_build_your_own_validators_in_aspnet_atlas.html

Validator是ASP.NET Atlas中的一类强大的组件,用来检查InputControl类型的Atlas控件,例如Web.UI.TextBox,的输入数据。如果你熟悉ASP.NET的话,你一定知道ASP.NET中作为服务器控件运行的Validator。Atlas中的Validator在客户端提供同样的功能。Atlas包含如下一些内建的Validator:

  1. requiredFieldValidator:检查是否有数据输入。
  2. typeValidator:检查输入的数据是否为特定的类型。
  3. rangeValidator:检查输入的值是否在一个范围之内。
  4. customValidator:用自定义的验证函数验证输入。
  5. regexValidator:用指定的正则表达式验证输入。

某个Atlas客户端控件的Validator可被定义成一个集合,当控件的propertyChanged事件被引发时,Atlas将调用Validator集合中的所有Validator去验证输入的数据。在验证的过程中一旦失败,这个Validator的validationMessage将被设置。Validator可以以组的形式验证一组控件的输入,并统一显示错误信息。

您还可以指定一个validationErrorLabel控件关联于某个将被验证的输入控件,它可以显示验证过程中的错误并可以自定义错误提示。

OK,以上内容大部分来自Atlas的文档,这里不再赘述。让我们通过一个IPAddressValidator的实例来学习如何编写自定义的Validator。顾名思义,IPAddressValidator将用于验证某个输入是否为一个合法的IPv4地址。

通常的,编写自定义Validator有如下两个步骤:

  1. 从Sys.UI.Validator基类中继承。
  2. 实现validate()方法来验证输入,并返回一个布尔值代表是否验证成功。

下面是IPAddressValidator的实现,并将其保存为IPAddressValidator.js。

ExpandedBlockStart.gif ContractedBlock.gif Sys.UI.IPAddressValidator  =   function ()  dot.gif {
InBlock.gif    Sys.UI.IPAddressValidator.initializeBase(
this);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.validate = function(value) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!value) dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (String.isInstanceOfType(value)) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (value.length == 0dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
var ipPattern = /^(\ddot.gif{1,3})\.(\ddot.gif{1,3})\.(\ddot.gif{1,3})\.(\ddot.gif{1,3})$/;
InBlock.gif        
var ipArray = value.match(ipPattern);
InBlock.gif
InBlock.gif        
if (ipArray == null)
InBlock.gif            
return false;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (i = 0; i < 4; i++dot.gif{
InBlock.gif            
var thisSegment = ipArray[i];
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (thisSegment > 255dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gifSys.UI.IPAddressValidator.registerSealedClass('Sys.UI.IPAddressValidator', Sys.UI.Validator);
None.gifSys.TypeDescriptor.addType('script', 'ipAddressValidator', Sys.UI.IPAddressValidator);

 

让我们在页面中测试上面的IPAddressValidator控件。这里将添加一个text box用于待验证IP地址的输入,一个label用于显示错误信息。

这里是ASPX中的HTML声明,不要忘记在ScriptManager中添加对上面IPAddressValidator.js文件的引用。

None.gif < atlas:ScriptManager  runat ="server"  ID ="ScriptManager1" >
None.gif    
< Scripts >
None.gif        
< atlas:ScriptReference  Path ="IPAddressValidator.js"   />
None.gif    
</ Scripts >
None.gif
</ atlas:ScriptManager >  
None.gif  
None.gif
< div  class ="description" >
None.gif    Please input an IP Address: 
None.gif    
< input  type ="text"  id ="ipBox"  class ="input"   />
None.gif    
< span  id ="ipValidator"  style ="color: red" > This IP Address is invalid! </ span >
None.gif
</ div >

 

 

下面是 Atlas 脚本定义:
None.gif < page  xmlns:script ="http://schemas.microsoft.com/xml-script/2005" >
None.gif    
< components >
None.gif        
< textBox  id ="ipBox" >
None.gif            
< validators >
None.gif                
< ipAddressValidator  errorMessage ="This IP Address is invalid!"   />
None.gif            
</ validators >
None.gif        
</ textBox >
None.gif        
< validationErrorLabel  id ="ipValidator"  associatedControl ="ipBox"   />
None.gif    
</ components >
None.gif
</ page >

 

在浏览器中运行结果:
validator1.JPG

validator2.JPG

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值