I need to validate a UITextField. I want only positive integers to be entered in it. How can I achieve that?
- 
                    Answered a [similar question here.](http://stackoverflow.com/a/12944946/868193) – Beltalowda Apr 24 '14 at 05:49
5 Answers
Add UITextFieldDelegate,and add this code to your class
#define LEAGELNUM @"0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEAGELNUM] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]     componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        return basicTest;
}
LEAGELNUM means you can only write these words
 
    
    - 2,038
- 1
- 14
- 15
- 
                    
- 
                    Hey what if I only want to limit one text box but use another to enter text? – Wez Apr 15 '12 at 11:50
- 
                    1@Wezly you can check whether the textField is the instance you need to limit. use `if(textField == xxx)` or use its tag like `if(textField.tag == xxx.tag)` xxx is your textField instance. – Bonny Apr 16 '12 at 01:28
- 
                    In case it is not known, `LEAGELNUM` is a mispelling. I think you meant `LEGALNUM`. – halfer Aug 29 '17 at 22:12
I'd set up a NSNumberFormatter to retrieve a number from the UITextFields text property. Then get an intValue from the number and check whether it's >= 0.
Update:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSNumber *number = [formatter numberFromString:[textField text]];
[formatter release];
This should get you going. I'm not on my development machine right now so I can't test run it, maybe you need to do one or two more lines of setup. Please refer to the reference library (I posted the link above) for the available options.
 
    
    - 8,980
- 4
- 50
- 82
- 
                    Could you please put an example(by editing your answer) that how can I use NSFormatter to check if textfield has positive number only? – Nitish Aug 05 '11 at 09:56
- 
                    
Set delegate to your UITextField ivar. Set keyboardType property to UIKeyboardTypeNumberPad
Write this code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
    if ([string isEqualToString:@"-"]){  
        return NO;  
    }  
    return YES;  
}
 
    
    - 29,669
- 15
- 106
- 125
- 
                    What if the user has not entered a number. What if user has entered ) or -? – Nitish Aug 05 '11 at 09:52
- 
                    1
- 
                    Number pad has some special symbols. How to check if user has entered special symbols? – Nitish Aug 05 '11 at 09:54
- 
                    UIKeyboardTypeNumberPad doesn't have a ) character. as per if ([string isEqualToString:@"-"]) – ader Aug 05 '11 at 09:54
- 
                    1Wow, UIKeyboardTypeNumberPad doesn't have anything except numbers :). It will be only positive value. – beryllium Aug 05 '11 at 09:57
Use logic of this answer Toastor
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if (string.characters.count == 0) {
        return true
    }
    /// check number is postive
    if (textField == self.txtPay) {
        let formatter = NSNumberFormatter()
        formatter.locale = NSLocale.currentLocale()
        let findalString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        let number = formatter.numberFromString(findalString)
        return number?.integerValue > 0
    }
    return true
}
 
    
    - 1
- 1
 
    
    - 816
- 10
- 16
From the top of my head:
Convert the string into a integer using [textField.text intValue] and check it is positive.
I would do it in the - (BOOL)textFieldShouldEndEditing:(UITextField *)textField method from the UITextFieldDelegate protocol.
Cheers
 
    
    - 8,103
- 4
- 31
- 45
 
    