前言:
對于表單的提交事件中,經常在提交前,會驗證一下表單的內容是否為空,格式是否正確,
本篇講述一下框架中關于表單校驗事件的相關使用方法,
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中,
-(UIView*)requireTipLabel:(id)nameOrLabel;
//!觸發驗證,(內部點擊觸發) -(BOOL)exeRequire;
@end
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}$"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/106758.html
標籤:其他
