Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITextFields中设置验证_Ios_Objective C_Iphone_Validation_Uitextfield - Fatal编程技术网

Ios 如何在UITextFields中设置验证

Ios 如何在UITextFields中设置验证,ios,objective-c,iphone,validation,uitextfield,Ios,Objective C,Iphone,Validation,Uitextfield,我正在使用一些文本字段和文本字段验证。我在下面展示了我的代码,在这个代码中单击按钮事件和所有文本字段文本保存在web服务器上,在这个代码中验证只在空文本字段上运行。但我想更正电子邮件地址验证和所有文本字段字符长度固定。我尝试了很多次,但有些时间条件错误,有些时间不显示警报视图,有些时间不保存文本字段文本。怎么可能请帮忙,谢谢 我的代码 喜欢 - (IBAction)submit:(id)sender { if (![txname hasText]) { [self showAl

我正在使用一些文本字段和文本字段验证。我在下面展示了我的代码,在这个代码中单击按钮事件和所有文本字段文本保存在web服务器上,在这个代码中验证只在空文本字段上运行。但我想更正电子邮件地址验证和所有文本字段字符长度固定。我尝试了很多次,但有些时间条件错误,有些时间不显示警报视图,有些时间不保存文本字段文本。怎么可能请帮忙,谢谢

我的代码 喜欢

- (IBAction)submit:(id)sender {

   if (![txname hasText]) {
     [self showAlertView:@"Alert" message:@"name is empty"];
   }
   else if  (![txemail hasText])
  {
    [self showAlertView:@"Alert" message:@"email is empty"];
   }
  else if ([self isValidEmailAddress:txemail.text] == NO)
  {
  [self showAlertView:@"Alert" message:@"Invaildemail"];
  }
  else
  {
  // call webservice for succes
  }
创建alertcontroller

- (void)showAlertView:(NSString*)title message:(NSString*)message
{
UIAlertController* alertMessage = [UIAlertController
    alertControllerWithTitle:title
                     message:message
              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction
    actionWithTitle:@"OK"
              style:UIAlertActionStyleDefault
            handler:^(UIAlertAction* action){
            }];

[alertMessage addAction:yesButton];

[self presentViewController:alertMessage animated:YES completion:nil];
}
用于电子邮件验证

-  (BOOL)isValidEmailAddress:(NSString *)emailAddress
{
 //Create a regex string
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ;

//Create predicate with format matching your regex string
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:
                          @"SELF MATCHES %@", stricterFilterString];

//return true if email address is valid
return [emailTest evaluateWithObject:emailAddress];
}
已更新

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 if (textField == self.txname)
 {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
    return NO;
}

NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
 return YES;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
if(textField==self.txname)
{
//防止崩溃撤消错误–请参阅下面的注释。
if(range.length+range.location>textField.text.length)
{
返回否;
}
NSUInteger newLength=[textField.text length]+[string length]-range.length;
返回newLength您喜欢吗

- (IBAction)submit:(id)sender {

   if (![txname hasText]) {
     [self showAlertView:@"Alert" message:@"name is empty"];
   }
   else if  (![txemail hasText])
  {
    [self showAlertView:@"Alert" message:@"email is empty"];
   }
  else if ([self isValidEmailAddress:txemail.text] == NO)
  {
  [self showAlertView:@"Alert" message:@"Invaildemail"];
  }
  else
  {
  // call webservice for succes
  }
创建alertcontroller

- (void)showAlertView:(NSString*)title message:(NSString*)message
{
UIAlertController* alertMessage = [UIAlertController
    alertControllerWithTitle:title
                     message:message
              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction
    actionWithTitle:@"OK"
              style:UIAlertActionStyleDefault
            handler:^(UIAlertAction* action){
            }];

[alertMessage addAction:yesButton];

[self presentViewController:alertMessage animated:YES completion:nil];
}
用于电子邮件验证

-  (BOOL)isValidEmailAddress:(NSString *)emailAddress
{
 //Create a regex string
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ;

//Create predicate with format matching your regex string
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:
                          @"SELF MATCHES %@", stricterFilterString];

//return true if email address is valid
return [emailTest evaluateWithObject:emailAddress];
}
已更新

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 if (textField == self.txname)
 {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
    return NO;
}

NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
 return YES;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
if(textField==self.txname)
{
//防止崩溃撤消错误–请参阅下面的注释。
if(range.length+range.location>textField.text.length)
{
返回否;
}
NSUInteger newLength=[textField.text length]+[string length]-range.length;

return newLength我是在代码中完成的,请按照以下方式执行:

    - (IBAction)submit:(id)sender {

        if (![self isFormValid]) {

            return;

        }

    NSError *error;


    if (!error)
    {
        UIAlertView *signupalert = [[UIAlertView alloc]initWithTitle:@"Congratulations" message:@"Record Added Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [signupalert show];

    }

}

-(BOOL)isFormValid
{

    NSString *emailRegEx =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegEx];



    if (txname.text && txname.text.length==0)
    {
        [self showErrorMessage:@"Please enter name"];
        return NO;
    }

    else if (tx_phone.text && tx_phone.text.length!=10)
    {
        [self showErrorMessage:@"Please enter valid phone number"];
        return NO;
    }

    else if([emailTest evaluateWithObject: txemail.text]==NO)
    {
        [self showErrorMessage:@"Please enter Valid Email_id"];
        return NO;
    }
    else if (txcomment.text && txcomment.text.length==0)
    {
        [self showErrorMessage:@"Please enter comment"];
        return NO;
    }

    return YES;
}

-(void)showErrorMessage:(NSString *)message
{

        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertmessage show];

    }

我是在我的代码中完成的,请按照以下方式执行:

    - (IBAction)submit:(id)sender {

        if (![self isFormValid]) {

            return;

        }

    NSError *error;


    if (!error)
    {
        UIAlertView *signupalert = [[UIAlertView alloc]initWithTitle:@"Congratulations" message:@"Record Added Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [signupalert show];

    }

}

-(BOOL)isFormValid
{

    NSString *emailRegEx =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegEx];



    if (txname.text && txname.text.length==0)
    {
        [self showErrorMessage:@"Please enter name"];
        return NO;
    }

    else if (tx_phone.text && tx_phone.text.length!=10)
    {
        [self showErrorMessage:@"Please enter valid phone number"];
        return NO;
    }

    else if([emailTest evaluateWithObject: txemail.text]==NO)
    {
        [self showErrorMessage:@"Please enter Valid Email_id"];
        return NO;
    }
    else if (txcomment.text && txcomment.text.length==0)
    {
        [self showErrorMessage:@"Please enter comment"];
        return NO;
    }

    return YES;
}

-(void)showErrorMessage:(NSString *)message
{

        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertmessage show];

    }


你在哪里实施这种方法你在哪里实施这种方法methods@AnkurKumawat-我将
作为宏使用,但在此处修改为
确定
的原因,请检查更新的answer@AnkurKumawat-字符长度。。?where@AnkurKumawat-这意味着你也需要这个条件。这个时间条件是正确的,但我还有一个条件事物字符长度fixed@AnkurKumawat-您的评论不清楚,请检查更新答案once@AnkurKumawat-我将
作为宏使用,但在此处修改为
确定
的原因,请检查更新的answer@AnkurKumawat-字符长度。。?where@AnkurKumawat-这意味着你需要这个条件,这一次也需要纠正c条件是正确的,但我还包括一件事字符长度fixed@AnkurKumawat-您的注释不清楚,请检查未声明标识符的更新答案:-isFormValid@Ankur更新了我的代码中的小更改,请检查一次..解决您的问题..:)它工作正常。谢谢,但名称文本字段字符长度如何设置(txname.text&&txname.text.length==0)在此处设置为bro@AnkurKumawatUndeclared标识符:-isFormValid@Ankur更新了我的代码中的小更改,请检查一次..这解决了您的问题..:)它工作正常。谢谢,但是名称文本字段字符长度如何设置如果(txname.text&&txname.text.length==0)在这里设置bro@AnkurKumawat