前言:对于表单的提交事件中,经常在提交前,会验证一下表单的内容是否为空,格式是否正确。 本篇讲述一下框架中关于表单校验事件的相关使用方法。 1、方法定义:@interface UIView (STUIViewValue) #pragma mark 设置数据校验 //!【表单设置】用于校验输入的必填、格式。(点击事件时被检测触发) -(UIView*)require:(BOOL)yesNo; -(UIView*)require:(BOOL)yesNo regex:(NSString*)regex; -(UIView*)require:(BOOL)yesNo regex:(NSString*)regex tipName:(NSString*)tipName; //!用于校验的分组触发(表单、按钮可设置)。 -(UIView*)requireGroup:(NSString*)name; #pragma mark 触发数据校验 //!【按钮设置】点击事件设置是否触发验证。 -(UIView*)requireBeforeClick:(BOOL)yesNo; //!【按钮设置】若需要将提示语显示在指定人UILabel中。 //!触发验证。(内部点击触发) -(BOOL)exeRequire; 2、基础方法使用:(判断不能为空、格式错误)添加完UIView后,增加require属性设置即可。 [[[[[sagit addTextField:@"UserName" placeholder:@"手机号码" font:0 color:MainFontColor] width:372 height:68] onRight:STPreView x:30 y:-10] require:YES regex:RexMobile] requireGroup:@"aa"]; 可以指定分组(如果不同的按钮事件需要触发不同的验证的话),否则可以不指定。 再添加一个: [[[[[sagit addTextField:@"password" placeholder:@"密码" font:0 color:MainFontColor] width:372 height:68] onRight:STPreView x:30 y:-10] require:YES regex:RexPassword] requireGroup:@"bb"]; 这里示例特意加上了requireGoup的用法,一般情况不需要分组没用到。 按钮点击事件:(指定requireBeforeClick:YES),需要触发分组时,可以指定分组(多个可以用逗号分隔) [[[[[sagit addButton:@"Login" title:@"登录" font:40] width:450 height:70] onBottom:@"pwdLine" y:149] requireBeforeClick:YES] requireGroup:@"aa,bb"]; - (void)LoginClick:(UIButton *)sender { // if(![self isMatch:@"手机号" name:@"UserName" regex:RexMobile] // || ![self isMatch:@"密码" name:@"password" regex:nil]) // { // return; // } NSMutableDictionary *para = [self formData]; [para setValue:@(UserAccountType) forKey:@"AccountType"]; [self.http post:UrlLogin paras:para success:^(STHttpModel *result) { if (result.success) { Sagit.Global.Token=(NSString *)result.msg; [STNew(@"MainController") asRoot]; }else { [self.msgBox prompt:(NSString *)result.msg]; } }]; } 被注释掉的是以前的写法,现在都在配置里实现,不需要写了。 运行效果:
提示语需要外置到UILabel显示时,对按钮指定属性即: [STLastButton requireTipLabel:label名字或Label本身]
3、高级使用方法:判断两个输入框值一致、图形验证码输入框是否正确对于正则的输入,除了可以输入正常的正则外,即"^.......$"。 也可以输入其它UIView的name名称。 当输入的不是正则,而是其它uiview的name的时候,则触发输入框一致、或图型验证码对错验证。 这里就不上图了。 总结:通过将基础验证归到属性设置,可以简少大量业务代码的判断。 上面示例用到的两个正则的宏定义: #pragma mark 正则 //手机号 #define RexMobile @"^1[3456789]\\d{9}$" //密码验证 #define RexPassword @"^[A-Za-z0-9]{6,16}$"
|
|