简体   繁体   中英

How to validate Textfield inputs

I have one module handling database calling function ie I have one textfield where user can enter their mobile number. When user enter first 4 digits of mobile number the other filed called Operator which is automatically change according to the 4 digit input value from that textfield.However my core functionality working good when user enter valid phone Numbers. My App getting crash when user enter invalid Number in textfield. I don't want my app to be crashed when user enter invalid inputs in textfields

my sample code:

 if(MobileNum.text.length ==4)
    {
        NSLog(@"MobileNum==>%@" ,MobileNum.text);

        Database *obj = [[Database alloc]init];
        NSMutableArray *dbArrray = [[NSMutableArray alloc]init];

        dbArrray =[obj getallRecords:MobileNum.text];

        NSString *value1 = [[dbArrray valueForKey:@"Operator"]objectAtIndex:0];

        if ([value1 isEqualToString:@""]) {
            NSString *data = @"Select Operator";
            NSString *data1 = @"Select Circle";

            operatorStr = [NSString stringWithFormat:@"%@", data];
            [operatorButton setTitle:operatorStr forState:UIControlStateNormal];
            circleStr = [NSString stringWithFormat:@"%@",data1];
            [circleButton setTitle:circleStr forState:UIControlStateNormal];
        }
        else
        {
            operatorStr = [[dbArrray valueForKey:@"Operator"]objectAtIndex:0];
            opString = operatorStr;
            NSLog(@"Operator Field==>%@",opString);
            [operatorButton setTitle:operatorStr forState:UIControlStateNormal];
            circleStr = [[dbArrray valueForKey:@"Circle"]objectAtIndex:0];
            cirString = circleStr;
            NSLog(@"Circle Field==>%@",cirString);
            [circleButton setTitle:circleStr forState:UIControlStateNormal];
        }
    }

and my textfield accepts only starting digit like 7,8,9 rest all inputs its rejects and app getting crashed

do this

dbArrray =[obj getallRecords:MobileNum.text];

 if (dbArrray.count==0)
{
  // do nothing , like select the another user

  [operatorButton setTitle:@"Select Operator" forState:UIControlStateNormal];           
  [circleButton setTitle:@"Select Circle" forState:UIControlStateNormal];
 }
else
 {

   [operatorButton setTitle:[[dbArrray valueForKey:@"Operator"]objectAtIndex:0] forState:UIControlStateNormal];
   [circleButton setTitle:[[dbArrray valueForKey:@"Circle"]objectAtIndex:0] forState:UIControlStateNormal];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM