Search code examples
iosregexvalidationuitextfielddelegate

validating uitextbox so it only sends back number between 0.5 and 24


Hi i am using the following code so my system validates only numbers to be entered into my text boxes this is fine. However, i just realised on one textfield i i would need it to validate only numbers between from 0.5 to 24, probably using regex, the stumbling block is even if figure out how to get the regex correct how would i incorportate into the function, which both only allows numbers and only allows number from .5 to 24. basically people can choose to use a device between 30 mins to 24 hours in a day, any field left as 0 results in an error.

what i have tried to do here is trap textfield2 which is the field to be set up to be maximum 24. The problem i have is even how do i limit the number and if ic an trap it how do i return it back

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range      replacementString:(NSString *)string
        {
    // First, create the text that will end up in the input field if you'll return     YES: 
        if (textField == textField2) {   }
        NSString *resultString = [textField.text stringByReplacingCharactersInRange:range     withString:string];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];   
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber* resultingNumber =     [numberFormatter numberFromString:resultString];       
       //[numberFormatter release];
        return     resultingNumber != nil;
   }

Solution

  • if (num2 == 0 || num1 <= 0.000|| num3 == 0 || num4 == 0 || dnumb ==0 || num2 > 24) { in a completley different part of the code, it pulls up a messagebox, the validation i need is simply done here no need for regex, – Imran Raja yesterday