0
3 개의 UITextField를 확인하려고하는데 11 문자 이상을 허용하는 텍스트 필드가 없습니다. 12 번째 문자를 입력하면 텍스트 필드가 빨간색으로 바뀌고 다음 필드로 이동하여 입력 할 수 없습니다.UITextField 유효성 검사 오류
나는이 링크 TextField Validation With Regular Expression의 유효성 검사에서 약간의 도움을 받았지만 이제는 왜 모든 textfield가 11 자 이상을 허용하지 않는지 이해할 수없는 것 같습니다. 당신이 포인터하지 문자열 내용을 비교하는 문자열의 사본 포인터가 다를 수 있으므로이있을 것이기 때문에 당신이 문자열을 비교하는 ==
를 사용하는 모든 곳
- (BOOL)validateInputWithString:(NSString *)aString
{
NSString* regularExpression = @"";
if (aString == self.ipAddress.text) {
regularExpression = @"^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
} else if (aString == self.username.text) {
regularExpression = @"^[A-Za-z][A-Za-z0-9]*$";
} else if (aString == self.password.text) {
regularExpression = @"^[^\\s\\r\\n][0-9a-zA-Zk\\r\\[email protected]!#\\$\\^%&*()+=\\-\\[\\]\\\\\\';,\\.\\/\\{\\<>\\?\\s]*$";
}
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: regularExpression
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
NSLog(@"error %@", error);
}
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString
options:0
range:NSMakeRange(0, [aString length])];
if([self.ipAddress.text length]>0 && [self.username.text length]>0 && [self.password.text length]>0)
{
enableLogin = YES;
}
return numberOfMatches > 0;
}
- (void)validateInputCallback:(NSNotification *) notification
{
UITextField* atextField = (UITextField *)[notification object];
if ([self validateInputWithString:atextField.text]){
//Turns the textfield green
atextField.layer.cornerRadius=8.0f;
atextField.layer.masksToBounds=YES;
atextField.layer.borderColor=[[UIColor greenColor]CGColor];
atextField.layer.borderWidth= 1.0f;
} else {
//Turns the textfield red
atextField.layer.cornerRadius=8.0f;
atextField.layer.masksToBounds=YES;
atextField.layer.borderColor=[[UIColor redColor]CGColor];
atextField.layer.borderWidth= 1.0f;
}
}