Allow only Numbers for UITextField input Allow only Numbers for UITextField input ios ios

Allow only Numbers for UITextField input


This is how you might handle the problem on a SSN verification field, you can modify the max length and remove the if statement checking for keyboard type if you need to.

There is also logic to suppress the max length alerts when the user is typing as opposed to pasting data.

Within the context of this code, presentAlert()/presentAlert: is just some basic function that presents a UIAlertController (or a legacy UIAlertView) using the message string passed.

Swift 5

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the // object that will contain this code, because otherwise it would never be called.//// There are also some better stylistic approaches in Swift to avoid all the // nested statements, but I wanted to keep the styles similar to allow others // to contrast and compare between the two languages a little easier.func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {    // Handle backspace/delete    guard !string.isEmpty else {        // Backspace detected, allow text change, no need to process the text any further        return true    }    // Input Validation    // Prevent invalid character input, if keyboard is numberpad    if textField.keyboardType == .numberPad {        // Check for invalid input characters        if CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) {            // Present alert so the user knows what went wrong            presentAlert("This field accepts only numeric entries.")            // Invalid characters detected, disallow text change            return false        }    }    // Length Processing    // Need to convert the NSRange to a Swift-appropriate type    if let text = textField.text, let range = Range(range, in: text) {        let proposedText = text.replacingCharacters(in: range, with: string)        // Check proposed text length does not exceed max character count        guard proposedText.count <= maxCharacters else {            // Present alert if pasting text            // easy: pasted data has a length greater than 1; who copy/pastes one character?            if string.count > 1 {                // Pasting text, present alert so the user knows what went wrong                presentAlert("Paste failed: Maximum character count exceeded.")            }            // Character count exceeded, disallow text change            return false        }        // Only enable the OK/submit button if they have entered all numbers for the last four        // of their SSN (prevents early submissions/trips to authentication server, etc)        answerButton.isEnabled = (proposedText.count == 4)    }    // Allow text change    return true}

Objective-C

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the // object that will contain this code, because otherwise it would never be called.- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    // Handle backspace/delete    if (!string.length)    {        // Backspace detected, allow text change, no need to process the text any further        return YES;    }    // Input Validation    // Prevent invalid character input, if keyboard is numberpad    if (textField.keyboardType == UIKeyboardTypeNumberPad)    {        if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound)        {            [self presentAlert: @"This field accepts only numeric entries."];            return NO;        }    }    // Length Validation    NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];    // Check proposed text length does not exceed max character count    if (proposedText.length > maxCharacters)    {        // Present alert if pasting text        // easy: pasted data has a length greater than 1; who copy/pastes one character?        if (string.length > 1)        {            // Pasting text, present alert so the user knows what went wrong            [self presentAlert: @"Paste failed: Maximum character count exceeded."];        }        // Character count exceeded, disallow text change        return NO;    }    // Only enable the OK/submit button if they have entered all numbers for the last four    // of their SSN (prevents early submissions/trips to authentication server, etc)    self.answerButton.enabled = (proposedText.length == maxCharacters);    // Allow text change    return YES;}


You can use this code to allow only number in textField.

Before that set delegate for textField

      textFieldName.delegate=self;

or

      [textFieldName setDelegate:self];

Than use this code to allow only digit to textField

      - (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {//return yes or no after comparing the characters      // allow backspace      if (!string.length)      {           return YES;      }      ////for Decimal value start//////This code use use for allowing single decimal value      //    if ([theTextField.text rangeOfString:@"."].location == NSNotFound)      //    {      //        if ([string isEqualToString:@"."]) {      //            return YES;      //        }      //    }      //    else      //    {      //        if ([[theTextField.text substringFromIndex:[theTextField.text rangeOfString:@"."].location] length]>2)   // this allow 2 digit after decimal       //        {      //            return NO;      //        }      //    }      ////for Decimal value End//////This code use use for allowing single decimal value      // allow digit 0 to 9      if ([string intValue])      {            return YES;      }      return NO;    }


Try this to avoid textfield clearing issue

Swift 3.0

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {        return false    }    return true}

Swift 4.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {    guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {        return false    }    return true}