代码段
NSString *myRegex = @"[A-Z0-9a-z]*";
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex];
NSString *string = [NSString stringWithString:NewPassword.text];
BOOL valid = [myTest evaluateWithObject:string];
我想验证 的 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 那些东西只是
假设客户输入特殊字符(@,#,&,。,},{,*,:等)。 无需验证和接受字符
前:
密码:1234567 密码:qwerty - 需要显示提醒
欲望输出: 密码: 123qwerty45 - 无需显示警告
密码:1234qwer @#$% - 接受密码
无需检查特殊字符
提前致谢
答案 0 :(得分:2)
尝试使用以下方法:
- (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
}
}
答案 1 :(得分:2)
以下代码可能会对您有所帮助。
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];
if ([NewPassword.text rangeOfCharacterFromSet:set].location == NSNotFound)
{
NSLog(@"NO SPECIAL CHARACTER");
}
else
{
NSLog(@"HAS SPECIAL CHARACTER");
}
答案 2 :(得分:0)
以下方法可以帮助您:
NSString *string = iTextField.text;
NSCharacterSet *alphanumSet = [NSCharacterSet alphanumCharSet];
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharSet];
BOOL isValid= [[string stringByTrimmingCharactersInSet:alphanumSet] isEqualToString:@""] && ![[string stringByTrimmingCharactersInSet:numberSet] isEqualToString:@""];
答案 3 :(得分:0)
要限制在文本字段中输入数据,您应该使用**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;
}
其中VALID_CHARECTERS可以是您想要包含的任何内容
答案 4 :(得分:0)
假设#$%是您想要的特殊字符。 您至少需要一个特殊字符,可以将其与字母数字字符混合使用。
你的正则表达式将是这样的: ([A-Z0-9a-Z] *(#| $ |%)+ [A-Z0-9a-Z] *)+
答案 5 :(得分:0)
- (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;
}
让我知道它是否正常工作!!
快乐编码!!!