我有我的應用程序的UITextField限制,iphone
1.username 2.Email 4個文本框 3,年齡 4.Password
用戶名3-25個字符,並且只含有字符[a-z0-9]
年齡必須介於1到100之間。
密碼是4-12個字符之間,並且只使用字符[A-ZA-Z0-9]
我怎麼能限制與上述規定
文本字段請人幫我做這..
謝謝你的努力和考慮。
我有我的應用程序的UITextField限制,iphone
1.username 2.Email 4個文本框 3,年齡 4.Password
用戶名3-25個字符,並且只含有字符[a-z0-9]
年齡必須介於1到100之間。
密碼是4-12個字符之間,並且只使用字符[A-ZA-Z0-9]
我怎麼能限制與上述規定
文本字段請人幫我做這..
謝謝你的努力和考慮。
您可以使用UITextFieldDelegate
協議中的方法來驗證字段的內容。
更具體地說,無論是使用:
– textFieldShouldEndEditing:
- textFieldShouldReturn:
或可以使用:
- textField:shouldChangeCharactersInRange:replacementString:
在第一情況下,僅當用戶結束編輯文本字段驗證;在第二種情況下,您可以在每次擊鍵時進行驗證。
在所有的這些方法,您會收到一個參數textField
,你可以像這樣訪問:
NSString* text = textField.text;
NSUInterger length = [text length];
if (length.....) {
// -- show alert or whatever
return NO;
}
您可以通過實現-[UITextField textField:shouldChangeCharactersInRange:replacementString:]
方法驗證號碼作爲用戶類型。請注意,在發生更改之前,此方法被稱爲,因此您需要構建可能是用戶操作結果的文本。例如:
-(BOOL)textField:(UITextField*)textField: shouldChangeCharactersInRange:(NSRange*)range
replacementString:(NSString*)string;
{
NSString* text = [textField.text stringByReplacingCharactersInRange:range
withString:string];
// text is now the potential string you should check against.
}
你從那裏做什麼取決於你自己。一些例子可以是:
// Too short?
if ([text length] < 4) ...
// Invalid character?
NSCharacterSet* invalidChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
if ([text rangeOfCharacterInSet:invalidChars].location != NSNotFound) ...
對於更復雜的號驗證我會用NSNumberFormatter
,具有用於驗證範圍和更多的支持。
上面的代碼顯示我一個error.location未聲明 – Prajan
你可以使用UITextFieldDelegate來完成你想要的任務。爲- (void)viewDidLoad
方法中的每個字段分配不同的值到textfield.tag,並匹配這些標記值以找到(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
中的相關字段。
#define USERNAME_FIELD_TAG 1
#define PASSWORD_FIELD_TAG 2
#define EMAIL_FIELD_TAG 3
#define AGE_FIELD_TAG 4
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tab == USERNAME_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[a-z0-9]{3,35}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == PASSWORD_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[a-zA-Z0-9]{4,12}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == EMAIL_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == AGE_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[1-100]"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
return YES;
}
// place the cursor at given possition
-(void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
offset:range.location];
UITextPosition *end = [input positionFromPosition:start
offset:range.length];
[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}
:但我只想A-Z和0-9的用戶名。我該怎麼辦? – Prajan
使用'NSRegularExpression'爲:http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html – sergio