我創建了UITextField
,我想對textfield(userName)進行驗證,以便只允許使用字符。如果用戶輸入除字母之外的其他任何內容(如數字或特殊字符),則不應出現,並且應該有通知顯示相同內容。在iOS中對UITextField進行驗證
-1
A
回答
0
你可以試試這個:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField==YOUR_TEXTFIELD_NAME)
{
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Numbers not allowed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return NO;
}
}
}
return YES;
}
請務必設置委託爲您文本框。
-1
try this
NSCharacterSet *blockedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
return ([characters rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);
}
-1
#define ACCEPTABLE_NUMBER @"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *acceptedInput = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_NUMBER]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:acceptedInput] componentsJoinedByString:@""];
if ((![filtered isEqualToString:string]))
return NO;
}
}
3
限定ALPHA @ 「ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz」
- (BOOL)的TextField:(的UITextField *)的TextField shouldChangeCharactersInRange:(NSRange)範圍replacementString:(的NSString *)串{
NSCharacterSet *blockedCharacters = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
if (!([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound)) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Only Alphabet allow." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
}
return ([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);
}
相關問題
- 1. UITextField驗證
- 2. 的UITextField驗證
- 3. UIAlertController UITextField驗證
- 4. 驗證空UITextField?
- 5. 驗證,UITextField
- 6. 驗證UITextField
- 7. 使用驗證控件對日期進行驗證?在Asp.net中?
- 8. 對錶格進行驗證
- 9. 對數組進行驗證
- 10. 對viewmodel + Catel進行驗證
- 11. Swift3 UITextField在按下按鈕後進行UIAlert驗證
- 12. 在WebForms中進行驗證
- 13. iOS:使用XSD進行XML驗證
- 14. 從iOS進行身份驗證
- 15. 對Java中的xsd進行XML驗證
- 16. iPhone:的UITextField驗證
- 17. 驗證多個uitextfield
- 18. UITextField驗證錯誤
- 19. 對錶格行進行驗證
- 20. NSXMLParser和針對iOS上的DTD進行驗證
- 21. 使用電話號碼對Firebase進行身份驗證iOS
- 22. 如何在ios中進行登錄驗證
- 23. 在iOS中使用XMPP框架進行匿名身份驗證
- 24. 在iOS中使用LDAP進行身份驗證
- 25. 如何在ios中使用SOAP服務進行身份驗證?
- 26. 在iOS應用程序中進行身份驗證的方法
- 27. 使用UITabBarController在iOS中進行身份驗證
- 28. 如何使用OAuth在Parse iOS SDK中對用戶進行身份驗證?
- 29. 如何在進行反編組時對XML進行驗證?
- 30. 在jQuery中對動態添加的表格行進行驗證
感謝您的重播,但此代碼也允許 – user3291536
1.您是否已將您的UITextField與IBOutlet連接? 2.您是否將UITextFieldDelegate設置爲您的文本字段? – GenieWanted
謝謝我得到答案 – user3291536