Skip to content

Instantly share code, notes, and snippets.

@satoshin2071
Created February 24, 2014 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoshin2071/9187188 to your computer and use it in GitHub Desktop.
Save satoshin2071/9187188 to your computer and use it in GitHub Desktop.
[iOS]UITextViewでの入力チェック
#define MAX_MEMO_LENGTH 32
#define MEMO_REGEX @"^[ぁ-んァ-ヶーa-zA-Z0-9一-龠0-9、。,. ]+$"
.
.
..
/*
最後に頭と末尾の空白文字取り除くのが望ましい
NSMutableString *str = [NSMutableString stringWithString:_textView.text];
CFStringTrimWhitespace((__bridge CFMutableStringRef)str);
_textView.text = nil;
*/
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *str = [textView.text stringByReplacingCharactersInRange:range withString:text];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:MEMO_REGEX options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *matches = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
//TODO:アラート文言?
// 最大数チェック
if ([str length] > MAX_MEMO_LENGTH) {
[UIAlertView showAlertViewWithTitle:@"エラー" message:@"申し訳ございません。\n文字数オーバーです" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:nil];
return NO;
// 正規表現でチェック
}else if([str length] > 0 && matches==nil){
[UIAlertView showAlertViewWithTitle:@"エラー" message:@"申し訳ございません。\nあまり特殊な記号と改行は使えません。" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:nil];
return NO;
}
[self.navigationItem.leftBarButtonItem setEnabled:YES];
return YES;
}
@satoshin2071
Copy link
Author

NSStringにするなら特に必要ないかも

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment