Iphone 验证ios中的文本字段和文本

Iphone 验证ios中的文本字段和文本,iphone,ios,regex,validation,Iphone,Ios,Regex,Validation,我在验证文本字段时遇到问题 我的问题是,我有3个文本字段 1.名称(我想要8到20个字符,包括大小写字母) 2.电子邮件Id(有效的电子邮件Id) 3.密码(应为强密码,即8至20个字符,包括小写、大写、数字和至少一个特殊字符) 我已经用regx解决了前两个条件。我在密码验证条件中遇到了问题,当我在密码中使用相同的regx时,我输入了正确的一个,它也显示了错误的密码警报。 我正在使用下面的方法 NSString *passwordRegex =@" ^.*(?=.{8,})(?=.*d)(?=.

我在验证文本字段时遇到问题

我的问题是,我有3个文本字段

1.名称(我想要8到20个字符,包括大小写字母)

2.电子邮件Id(有效的电子邮件Id)

3.密码(应为强密码,即8至20个字符,包括小写、大写、数字和至少一个特殊字符)

我已经用regx解决了前两个条件。我在密码验证条件中遇到了问题,当我在密码中使用相同的regx时,我输入了正确的一个,它也显示了错误的密码警报。 我正在使用下面的方法

NSString *passwordRegex =@" ^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
BOOL bool = [passwordTest evaluateWithObject:password.text];

if (bool==NO) {
    UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Title", nil) message:NSLocalizedString(@"Invalid Password", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];[messageBox show];
    [messageBox release]; 
    passwordValid=0;
}else passwordValid=1;
提前感谢

尝试此代码

BOOL passwordValid;
NSString *passwordRegex =@"^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
NSString *stringWithPass = @"C0mp@redText";
BOOL isPasswordValid = [passwordTest evaluateWithObject:stringWithPass];

if (isPasswordValid==NO) {
    UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Title", nil) message:NSLocalizedString(@"Invalid Password", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
    [messageBox show];
    passwordValid=0;
}else passwordValid=1;
我认为您必须从字符串开始的位置删除空格
^

请尝试以下代码

BOOL passwordValid;
NSString *passwordRegex =@"^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
NSString *stringWithPass = @"C0mp@redText";
BOOL isPasswordValid = [passwordTest evaluateWithObject:stringWithPass];

if (isPasswordValid==NO) {
    UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Title", nil) message:NSLocalizedString(@"Invalid Password", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
    [messageBox show];
    passwordValid=0;
}else passwordValid=1;
我认为您必须从字符串开始的位置删除空格
^

尝试以下操作:

    ^.*(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$
看看这个

试试这个:

    ^.*(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$
看看这个


在您的代码中,您是否按原样使用了
BOOL BOOL
?你不应该使用关键字
bool
作为变量名。我不能这样使用。我在你的代码中使用了“bool b”,你是按原样使用
bool bool
吗?你不应该用关键字
bool
作为变量名。我不能那样用。我用的是“bool b”