Limit Uitextfield Input to Numbers in Swift

How to allow only certain set of numbers in a UITextfield in swift 2.0

Set keyboard type as Number Pad

add this

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

if let text = textField.text {

let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
return (intvalue >= 0 && intvalue <= 12)
}
return true
}

Limit UITextField input to numbers in Swift

You can use UITextFieldDelegate’s shouldChangeCharactersInRange method to limit the user's input to numbers:

func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool {

// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersInString:"0123456789").invertedSet

// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.componentsSeparatedByCharactersInSet(inverseSet)

// Rejoin these components
let filtered = components.joinWithSeparator("") // use join("", components) if you are using Swift 1.2

// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
}

Updated for Swift 3:

 func textField(_ textField: UITextField, 
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {

// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersIn:"0123456789").inverted

// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.components(separatedBy: inverseSet)

// Rejoin these components
let filtered = components.joined(separator: "") // use join("", components) if you are using Swift 1.2

// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
}

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;
}

How to limit number of decimals in textfield when user input is from pasteboard, iOS Swift

This code always updates the content of textField but limits the number of decimals:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

guard let decimalSeparator = NSLocale.current.decimalSeparator else {return true}

// Updates the text
var updatedText = (textView.text as NSString).replacingCharacters(in: range, with: text)

// If someone needs to cover all possible decimal separator values, the commented line below is the solution
// let textComponents = updatedText.components(separatedBy: [",", "."])

let textComponents = updatedText.components(separatedBy: decimalSeparator)

// Truncates the decimals
if textComponents.count > 1 && textComponents[1].count > 6{
updatedText = textComponents[0].appending(decimalSeparator).appending((textComponents[1] as NSString).substring(to: 6))
}

textView.text = updatedText

// The text has already been updated, so returns false
return false
}

How to limit the numbers in the text field in Swift?

I think there are 2 ways you can do that.

Implement the UITextFieldDelegate and implement function

func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool {

var startString = ""

if textField.text != nil {
startString += textField.text!
}

startString += string

var limitNumber = startString.toInt()

if limitNumber > 60 {
return false
} else {
return true
}
}

In this Each time check what has been entered to the UITextField so far, convert to Integer and if the new value is higher than 60, return false. (Also show the appropriate error to the user).

I think a much better way would be to provide UIPickerView.

Set the maximum character length of a UITextField in Swift

  1. Your view controller should conform to UITextFieldDelegate, like below:

    class MyViewController: UIViewController, UITextFieldDelegate {

    }
  2. Set the delegate of your textfield: myTextField.delegate = self

  3. Implement the method in your view controller:

    textField(_:shouldChangeCharactersInRange:replacementString:)

All together:

class MyViewController: UIViewController, UITextFieldDelegate  // Set delegate to class

@IBOutlet var mytextField: UITextField // textfield variable

override func viewDidLoad() {
super.viewDidLoad()
mytextField.delegate = self // set delegate
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool
{
let maxLength = 4
let currentString: NSString = textField.text
let newString: NSString = currentString.stringByReplacingCharactersInRange(range, withString: string)

return newString.length <= maxLength
}

For Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 1
let currentString: NSString = (textField.text ?? "") as NSString
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

return newString.length <= maxLength
}

For Swift 5

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 1
let currentString = (textField.text ?? "") as NSString
let newString = currentString.replacingCharacters(in: range, with: string)

return newString.count <= maxLength
}

Allowing only a specified set of characters to be entered into a given text field

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var result = true

if mytextField == textField {
if count(string) > 0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789.-").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
result = replacementStringIsLegal
}
}

return result
}

How to program an iOS text field that takes only numeric input with a maximum length

How to limit decimal input value in UITextField

You probably need two checks:

  1. Make sure it in the form of xxx.xx. This sort of pattern matching is often achieved by using regular expression search.

    The trick here is to make sure you support all permutations with and without decimal place, where the fractional digits is two or fewer digits and the integer digits is three or fewer digits.

  2. Try converting it to a number and check that the value is less than 999.

Thus:

let formatter = NumberFormatter()

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let candidate = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
let separator = formatter.decimalSeparator!

if candidate == "" { return true }

let isWellFormatted = candidate.range(of: "^[0-9]{1,3}([\(separator)][0-9]{0,2})?$", options: .regularExpression) != nil

if isWellFormatted,
let value = formatter.number(from: candidate)?.doubleValue,
value >= 0,
value < 999 {
return true
}

return false
}

Note:

  • I’m assuming you want users to be able to honor their device’s localization settings (e.g. let a German user enter 123,45 because they use , as the decimal separator).

  • The regular expression, "^[0-9]{1,3}([\(separator)][0-9]{0,2})?$” probably looks a little hairy if you’re not used to regex.

    • The ^ matches the start of the string;
    • The [0-9] obviously matches any digit;
    • The {1,3} matches between one and three integer digits;
    • The (...)? says “optionally, look for the following”;
    • Again, [0-9]{0,2} means “between zero and two fractional digits; and
    • The $ matches the end of the string.


Related Topics



Leave a reply



Submit