Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift uitextfield only numbers keyboard lock programmatically

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // you can set the following two properties for the text field in Interface Builder, if you'd prefer

        textField.delegate = self
        textField.keyboardType = .numberPad
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let invalidCharacters = CharacterSet(charactersIn: "0123456789").inverted
        return string.rangeOfCharacter(from: invalidCharacters) == nil
    }

    // or, alternatively:
    //
    // func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    //     return string.range(of: "^d*$", options: .regularExpression) != nil
    // }

}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift connect wifi 
Swift :: swift string pad 
Swift :: corner radius with animation swift 
Swift :: Swift Hashable Protocol 
Swift :: swift computed property 
Swift :: library not found for -lalan-sdk-react-native 
Swift :: Swift Function Overloading 
Swift :: print 1 line swift 
Swift :: Swift continue Statement With for Loop 
Swift :: vibrations in ios swift 
Swift :: didselectrowatindexpath not called swift 
Swift :: move to nect cell of collection after some time automatically in ios swift 
Ruby :: rails undo scaffold 
Ruby :: activerecord list tables 
Ruby :: run rake task in rails console 
Ruby :: fibonacci sums ruby 
Ruby :: rails g model references 
Ruby :: ruby map with index 
Ruby :: require relative ruby 
Ruby :: edit file terminal mac 
Ruby :: ruby square root 
Ruby :: ruby generate uuid 
Ruby :: ruby loop through array 
Ruby :: ruby copy file 
Ruby :: ruby array 
Ruby :: run Rspec 
Ruby :: rails loop through datetime 
Ruby :: ruby loop using integer 
Ruby :: rails generate model with options 
Ruby :: * 16**index position in ruby 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =