I am making an app i want that when user inputs any data then whatever user enter like if he enters a so it does not take this input and show nothing in textfield and if it enter any number then it should accept.
            Asked
            
        
        
            Active
            
        
            Viewed 297 times
        
    1
            
            
        - 
                    4Why don't you use a numeric keypad then ? Make your keyboard type UIKeyboardTypeNumberPad – Midhun MP Mar 13 '13 at 08:34
- 
                    See the types of keyboard. You can set according to your need. http://stackoverflow.com/questions/7301018/programmatically-change-uitextfield-keyboard-type – Baby Groot Mar 13 '13 at 09:06
5 Answers
4
            Following code should help you, this will not allow any other text to be entered in your text field other than numbers.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    static NSCharacterSet *charSet = nil;
    if(!charSet) {
    charSet = [[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet] retain];
    }
    NSRange strLocation = [string rangeOfCharacterFromSet:charSet];
    return (strLocation.location == NSNotFound);
}
 
    
    
        Krishna Shanbhag
        
- 1,519
- 13
- 33
2
            
            
        Why do u do this better give a keyboard style
[txtField setKeyboardType:UIKeyboardTypeNumberPad]
or u can do it programatically :
NSString *nameRegex = @"[0-9]*";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
BOOL value = [nameTest evaluateWithObject:string];
if(value == YES)
    {
 // Do Somethings
    }
    else 
        {
    // Do something
    }
 
    
    
        Kasaname
        
- 1,491
- 11
- 15
0
            
            
        you need to set keyboard type of Textfield as Numberpad.
Try this using code : (also u can set with Inter face builder on XIB property)
[txtFieldObj setKeyboardType:UIKeyboardTypeNumberPad];
 
    
    
        Satish Azad
        
- 2,302
- 1
- 16
- 35
0
            
            
        -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag==3)
{
    NSUInteger lengthOfString = string.length;
    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++)
    {
        unichar character = [string characterAtIndex:loopIndex];
        if (character < 46 || character==47)
            return NO;
        if (character > 57)
            return NO;
    }
}
Set tag to your textField.
 
    
    
        Manu
        
- 4,730
- 2
- 20
- 45
0
            
            
         NSUInteger lengthOfString = string.length;
    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
        unichar character = [string characterAtIndex:loopIndex];
        if (character < 48) return NO; // 48 unichar for 0
        if (character > 57) return NO; // 57 unichar for 9
    }
this is the code should be written in textfield should change characters inrange
 
    
    
        MANCHIKANTI KRISHNAKISHORE
        
- 350
- 2
- 17
 
    