Question

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;
   }
Was it helpful?

Solution 3

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

OTHER TIPS

Use this regex (^([1][0-9]|[2][0-4]|(0?)[1-9])((.[0-9])?)|0[.][5-9][0-9]$) to validate the number between 0.5 to 24..

I've recently done something similar but will try to adapt it to your situation. If I foobared a line of code call me out before ripping me a new one... it's late ;)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSError *error = NULL;
    //check for text being pasted vs valid decimal
    //below regex will check for a decimal number with a precision of 2

    NSRegularExpression *regexNumeric = [[NSRegularExpression alloc]initWithPattern:@"^([0-9]+)?(\\.([0-9]{1,2})?)?$" options:NSRegularExpressionCaseInsensitive error:&error];
    if ([regexNumeric numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])] > 0)
    {
        //found a valid entry
        switch ([string length])
        case 0:
        case 1:
        {
            //this is always a valid entry since the field is either empty or 0-9 or .
            return YES;
            break;
        }
        case 2:
        {
            //Need to handle whole numbers, decimals starting with ".", and a decimal starting with "0." which is valid
            if ((([string floatValue] <= 24.0) && ([string floatValue] >= 0.5)) || ([string isEqualToString:@"0."]))
            {
                return YES;
            }
            else
            {
                return NO;
            }
            break;
        }
        case 3:
        case 4:
        case 5:
        {
            //will handle all other cases
            if (([string floatValue] <= 24.0) && ([string floatValue] >= 0.5))
            {
                return YES;
            }
            else
            {
                return NO;
            }
            break;
        }
        default:
        {
            //no idea how you got here!
            return NO;
        }
    }
    else
    {
        return NO;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top