Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 258 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to restrict UITextField to take only numbers in Swift?

#31
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let numRange = string.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet()) {
return false
} else {
return true
}
}

Reply

#32
While most of these solutions will work, be aware that in some localisations a decimals are separated with a "," and not a "."

The cleaner way to do this would be

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let decimalCharacter = NSNumberFormatter().decimalSeparator
let characterSet = NSMutableCharacterSet.decimalDigitCharacterSet()
characterSet.addCharactersInString(decimalCharacter)

return replacementString.rangeOfCharacterFromSet(characterSet.invertedSet) == nil
}
Reply

#33
This is a more readable version that will do "0-9" plus ".":

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

let existingTextHasDecimal = textField.text?.rangeOfString(".")
let replacementTextHasDecimal = string.rangeOfString(".")
let replacementTextAllCharacters = NSCharacterSet(charactersInString: string)
let replacementTextOnlyDigits = NSCharacterSet.decimalDigitCharacterSet().isSupersetOfSet(replacementTextAllCharacters)

if replacementTextHasDecimal != nil && existingTextHasDecimal != nil {
return false
}else{
if replacementTextOnlyDigits == true {
return true
}else if replacementTextHasDecimal != nil{
return true
}else{
return false
}
}
}
Reply

#34
Swift 2.0

For only allowing numbers and one "." decimal in uitextfield.

func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool
{
let newCharacters = NSCharacterSet(charactersInString: string)
let boolIsNumber = NSCharacterSet.decimalDigitCharacterSet().isSupersetOfSet(newCharacters)
if boolIsNumber == true {
return true
} else {
if string == "." {
let countdots = textField.text!.componentsSeparatedByString(".").count - 1
if countdots == 0 {
return true
} else {
if countdots > 0 && string == "." {
return false
} else {
return true
}
}
} else {
return false
}
}
}
Reply

#35
I had actually done this when working through the Big Nerd Ranch book, my solution is:

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

let newCharacters = NSCharacterSet(charactersInString: string)
return NSCharacterSet.decimalDigitCharacterSet().isSupersetOfSet(newCharacters)
}

this only allows the numbers 0-9, to allow the "." as well is more complicated as you can only allow one "."
Reply

#36
Swift 2.0

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

let inverseSet = NSCharacterSet(charactersInString:"0123456789").invertedSet

let components = string.componentsSeparatedByCharactersInSet(inverseSet)

let filtered = components.joinWithSeparator("")

return string == filtered

}
Reply

#37
First add `delegate` and `keyBoradType` of `textField`

`textField.delegate=self;
textField.keyboardType = UIKeyboardTypeNumberPad;`


Than have to use the `textField.delegate` method like so -

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string
{
if (!string.length)
{
return YES;
}

if ([string intValue])
{
return YES;
}

return NO;
}
Reply

#38

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// return true if the replacementString only contains numeric characters
let digits = NSCharacterSet.decimalDigitCharacterSet()
for c in string {
if !digits.characterIsMember© {
return false
}
}

return true
}

This solution will work even if the user switches keyboards or tries to paste a non-numeric string into the text field.

Make sure to set the `delegate` property of the appropriate text field.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through