[英]How to avoid special characters (@, #, &, etc.) in UITextfield validation
Code snippet 程式码片段
NSString *myRegex = @"[A-Z0-9a-z]*";
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex];
NSString *string = [NSString stringWithString:NewPassword.text];
BOOL valid = [myTest evaluateWithObject:string];
i want to validate abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 those things only 我只想验证abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
Suppose customer enter special characters (@, #, &,.,},{,*,:, etc.). 假设客户输入特殊字符(@,#,&..},{,* 、:等)。 no need to validate and accept the characters
无需验证和接受字符
Ex : 例如:
Password : 1234567 Password : qwerty - Need to show Alert 密码:1234567密码:qwerty-需要显示警报
Desire output : Password : 123qwerty45 - No need to show alert 需求输出:密码: 123qwerty45-无需显示警报
Password : 1234qwer@#$% -accept the password 密码:1234qwer @#$%-接受密码
No need to check special character 无需检查特殊字符
Thanks in advance 提前致谢
Try with following method: 请尝试以下方法:
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
if ([replacementString rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
// There are non-alphanumeric characters in the replacement string
}
}
Following code might help you. 以下代码可能会对您有所帮助。
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];
if ([NewPassword.text rangeOfCharacterFromSet:set].location == NSNotFound)
{
NSLog(@"NO SPECIAL CHARACTER");
}
else
{
NSLog(@"HAS SPECIAL CHARACTER");
}
The following method may help you: 以下方法可以为您提供帮助:
NSString *string = iTextField.text;
NSCharacterSet *alphanumSet = [NSCharacterSet alphanumCharSet];
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharSet];
BOOL isValid= [[string stringByTrimmingCharactersInSet:alphanumSet] isEqualToString:@""] && ![[string stringByTrimmingCharactersInSet:numberSet] isEqualToString:@""];
To restrict entering data in the textfield you should define it in a **shouldChangeCharactersInRange**
method 要限制在文本字段中输入数据,您应该在
**shouldChangeCharactersInRange**
方法中对其进行定义
#define VALID_CHARECTERS @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *validInput = [NSCharacterSet characterSetWithCharactersInString:VALID_CHARECTERS];
if (![[string componentsSeparatedByCharactersInSet:avalidInput] count] > 1)
return NO;
else
return YES;
}
where VALID_CHARECTERS can be anything you want to include VALID_CHARECTERS可以是您要包含的任何内容
Assume #$% are the special characters you want. 假设#$%是您想要的特殊字符。 You want at least one special character, with the possability of mixing it with alphanumerical characters.
您需要至少一个特殊字符,并可以将其与字母数字字符混合。
Your regular expression would then be something like: ([A-Z0-9a-z]*(#|$|%)+[A-Z0-9a-z]*)+ 这样,您的正则表达式将类似于:([A-Z0-9a-z] *(#| $ |%)+ [A-Z0-9a-z] *)+
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *ss = [[NSCharacterSet alphanumericCharacterSet]invertedSet];
if ([string isEqualToString:@""])
{
NSString *trimmedReplacement =
[[ string componentsSeparatedByCharactersInSet:ss]
componentsJoinedByString:@""];
}
else
{
return ([string stringByTrimmingCharactersInSet:ss].length > 0);
}
}
return YES;
}
let me know it is working or not!! 让我知道它是否有效!
Happy Coding!!! 快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.