Atlas学习手记(26):使用Validators验证用户输入

ValidatorAtlas提供的一组验证用户输入的客户端组件,用来检查InputControl类型的Atlas控件,例如Web.UI.TextBox的输入数据。在ASP.NET中提供了一组服务器端的验证控件,Atlas中的Validator在客户端也提供了同样的功能。

 

主要内容

1Validators概述

2.完整示例

 

一.Validators概述

ValidatorAtlas提供的一组验证用户输入的客户端组件,用来检查InputControl类型的Atlas控件,例如Web.UI.TextBox的输入数据。在ASP.NET中提供了一组服务器端的验证控件,Atlas中的Validator在客户端也提供了同样的功能。Atlas提供的Validator如下所示:

1requiredFieldValidator:检查是否有数据输入。

2typeValidator:检查输入的数据是否为特定的类型。

3rangeValidator:检查输入的值是否在一个范围之内。

4customValidator:用自定义的验证函数验证输入。

5regexValidator:用指定的正则表达式验证输入。

某个Atlas客户端控件的Validator可被定义成一个集合,当控件的propertyChanged事件被引发时,Atlas将调用Validator集合中的所有Validator去验证输入的数据。在验证的过程中一旦失败,这个ValidatorvalidationMessage将被设置。Validator可以以组的形式验证一组控件的输入,并统一显示错误信息。您还可以指定一个validationErrorLabel控件关联于某个将被验证的输入控件,它可以显示验证过程中的错误并可以自定义错误提示。[来自于Dflying的介绍]

二.完整示例

下面针对这几种Validator做几个简单的小例子。

1requiredFieldValidator

检测是否有有数据输入,用一个textbox接收用户输入,用一个label来显示错误信息:

None.gif < div >
None.gif
None.gif    
< h3 > Example 1: Required Field Validator </ h3 >
None.gif
None.gif    
< br  />
None.gif
None.gif    
< input  type ="text"  id ="value1TextBox"  class ="input"   />
None.gif
None.gif    
&nbsp;   < span  id ="validator1"  style ="color: red" > You must enter some text </ span >  
None.gif
None.gif    
< br  />
None.gif
None.gif    
< br  />
None.gif
None.gif    Text: 
< span  id ="value1Label"  class ="result" ></ span >
None.gif
None.gif    
< br  />
None.gif
None.gif
</ div >

编写Atlas脚本,分别用一个requiredFieldValidatorvalidtionErrorLabel,并且把用户输入的数据显示在一个label上,在validationErrorLabel中用associatedControl来关联要验证的控件:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/xml-script" > dot.gif
InBlock.gif
InBlock.gif    
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
InBlock.gif
InBlock.gif        
<components>
InBlock.gif
InBlock.gif         
<textBox id="value1TextBox">
InBlock.gif
InBlock.gif                
<validators>
InBlock.gif
InBlock.gif                    
<requiredFieldValidator  errorMessage="You must enter some text."/>
InBlock.gif
InBlock.gif                
</validators>
InBlock.gif
InBlock.gif         
</textBox>
InBlock.gif
InBlock.gif         
<validationErrorLabel id="validator1" associatedControl="value1TextBox" />
InBlock.gif
InBlock.gif         
<label id="value1Label">
InBlock.gif
InBlock.gif            
<bindings>
InBlock.gif
InBlock.gif                
<binding dataContext="value1TextBox" dataPath="text" property="text" />
InBlock.gif
InBlock.gif            
</bindings>
InBlock.gif
InBlock.gif         
</label>
InBlock.gif
InBlock.gif        
</components>
InBlock.gif
InBlock.gif    
</page>
ExpandedBlockEnd.gif
None.gif
</ script >

运行后界面如下:

输入数据,验证通过,没有提示错误信息:

输入为空,提示错误信息“You must enter some text”:

2typeValidator

检测用户输入的数据类型,在这个例子中我们验证用户输入的是否为数据:

None.gif < div >
None.gif
None.gif
< h3 > Example 2: Type Validator </ h3 >
None.gif
None.gif    
< br  />
None.gif
None.gif    
< input  type ="text"  id ="value2TextBox"  class ="input"   />
None.gif
None.gif    
< br  />
None.gif
None.gif    
< br  />
None.gif
None.gif    
< span  id ="validator2"  style ="color:red" > You must enter a valid number </ span >
None.gif
None.gif
</ div >

编写Atlas脚本,设置非常简单,指定typeNumber

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/xml-script" > dot.gif
InBlock.gif
InBlock.gif    
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
InBlock.gif
InBlock.gif        
<components>
InBlock.gif
InBlock.gif        
<textBox id="value2TextBox">
InBlock.gif
InBlock.gif                        
<validators>
InBlock.gif
InBlock.gif                            
<requiredFieldValidator errorMessage="You must enter a number." />
InBlock.gif
InBlock.gif                            
<typeValidator type="Number" errorMessage="You must enter a valid number" />
InBlock.gif
InBlock.gif                        
</validators>
InBlock.gif
InBlock.gif                    
</textBox>
InBlock.gif
InBlock.gif                    
<validationErrorLabel id="validator2" visibilityMode="Hide" associatedControl="value2TextBox" />
InBlock.gif
InBlock.gif        
</components>
InBlock.gif
InBlock.gif    
</page>
ExpandedBlockEnd.gif
None.gif
</ script >

编译运行,输入数字100,验证通过没有报错误信息:

输入TerryLee,提示“You must enter a valid number”错误信息:

3.regexValidator

用正则表达式来验证用户输入的数据,这里我们以验证用户录入的电话号码格式是否正确为例,添加相关的HTML元素:

None.gif < div >
None.gif    
< h3 > Example 3: RegEx Validator </ h3 >
None.gif
None.gif    
< input  type ="text"  id ="value3TextBox"  class ="input"   />
None.gif
None.gif    
< br  />
None.gif
None.gif    
< br  />
None.gif
None.gif    
< span  id ="validator3"  style ="color: red" > You must a valid phone number </ span >
None.gif
None.gif
</ div >

编写Atlas脚本,加入regexValidator,注意这儿在正则表达式的前后必须加入“/”?否则会报脚本错误:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/xml-script" > dot.gif
InBlock.gif
InBlock.gif    
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
InBlock.gif
InBlock.gif        
<components>
InBlock.gif
InBlock.gif        
<textBox id="value3TextBox">
InBlock.gif
InBlock.gif            
<validators>
InBlock.gif
InBlock.gif                
<requiredFieldValidator errorMessage="You must enter some text." />
InBlock.gif
InBlock.gif                
<regexValidator regex="/(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}/" errorMessage="You must a valid phone number" />
InBlock.gif
InBlock.gif            
</validators>
InBlock.gif
InBlock.gif        
</textBox>
InBlock.gif
InBlock.gif            
<validationErrorLabel id="validator3" visibilityMode="Collapse"
InBlock.gif
InBlock.gif            associatedControl
="value3TextBox" />
InBlock.gif
InBlock.gif        
</components>
InBlock.gif
InBlock.gif    
</page>
ExpandedBlockEnd.gif
None.gif
</ script >

编译运行,录入正确的电话号码:

输入错误格式的电话号码,会报“You must a valid phone number”错误:

本文就简单的介绍到这儿,其他的示例大家可以参考Atlas官方网站。

完整示例下载:http://files.cnblogs.com/Terrylee/ValidatorDemo.rar

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值