輸入控制:
/**
*? textFiled輸入變化的時(shí)候
*
*? @param textField
replacementString //鍵盤每次輸入的鍵
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//控制輸入個(gè)數(shù)
if (textField.tag == numberTextFieldTag) {
//判斷是否為刪除字符瘫辩,如果為刪除則讓執(zhí)行
if (string.length == 0) {
return YES;
}
//獲取輸入框內(nèi)字符串
NSString *text = textField.text;
//判斷是否有小數(shù)點(diǎn)
if ([text rangeOfString:@"."].length) {
//獲取小數(shù)點(diǎn)后面字符串長(zhǎng)度
if ([text substringFromIndex:[text rangeOfString:@"."].location].length > 2) {
return NO;
}else{
return YES;
}
}else{
return YES;
}
}
return YES;
}
提交時(shí):
NSString *text = self.redPacketNumberTextField.text;
//判斷輸入是否合規(guī)
//如果存在"."
if ([text rangeOfString:@"."].location != NSNotFound) {
if ([text hasPrefix:@"."] || [text hasSuffix:@"."]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:AppLocalizedString(@"Red Packet Amount error") delegate:self cancelButtonTitle:AppLocalizedString(@"OK") otherButtonTitles:nil, nil];
[alert show];
[self.redPacketNumberTextField becomeFirstResponder];
return;
}else{
//如果出現(xiàn)多個(gè)"."
NSString *str = [text substringFromIndex:[text rangeOfString:@"."].location + 1];
if ([str rangeOfString:@"."].location != NSNotFound) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:AppLocalizedString(@"Red Packet Amount error") delegate:self cancelButtonTitle:AppLocalizedString(@"OK") otherButtonTitles:nil, nil];
[alert show];
[self.redPacketNumberTextField becomeFirstResponder];
return;
}
//如果0開頭,且小數(shù)點(diǎn)前面不止一位
if ([text hasPrefix:@"0"]){
NSString *perfixStr = [text substringWithRange:NSMakeRange(1, 1)];
if (![perfixStr isEqualToString:@"."]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:AppLocalizedString(@"Red Packet Amount error") delegate:self cancelButtonTitle:AppLocalizedString(@"OK") otherButtonTitles:nil, nil];
[alert show];
[self.redPacketNumberTextField becomeFirstResponder];
return;
}
}
}
}else{
//如果沒有小數(shù)點(diǎn)
//如果0開頭
if ([text hasPrefix:@"0"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:AppLocalizedString(@"Red Packet Amount error") delegate:self cancelButtonTitle:AppLocalizedString(@"OK") otherButtonTitles:nil, nil];
[alert show];
[self.redPacketNumberTextField becomeFirstResponder];
return;
}
}