How to validate or Allow Only Numeric value TextField in IOS or objective-c
In whatever UITextField you’re getting these values from, you can specify the kind of keyboard you want to appear when somebody touches inside the text field.
- Check the maximum allowed characters.
- Check the valid decimal number.
- Check only numeric numbers.
- The code is the UITextField delegate method. Before you use this snippet, you must have these properties:
- self.maxCharacters
- self.numeric // Only int characters.
- self.decimalNumeric // Only numbers and “.”, “,”
Also Read : How to move UP the UIVIEW when keyboard is display or typing in UITEXTFIELD in IOS !!!
Objective C
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL allowChange = YES;
if ([textField isEqual:zipText])
{
allowChange = NO;
int textLength = [textField.text length];
int value;
if (([string isEqualToString:@""]))
{
allowChange=YES;
}
else
{
value = (int) [string characterAtIndex:0];
}
if ((textLength <= kMaxZipLength) && value >=48 && value <=57)
{
allowChange=YES;
}
}
return allowChange;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (!string.length)
return YES;
if (textField == self.tmpTextField)
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches == 0)
return NO;
}
return YES;
}
SWIFT
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if !string.characters.count {
return true
}
do {
if textField == self.tmpTextField {
var newString = textField.text.replacingCharacters(inRange: range, with: string)
var expression = "^([0-9]+)?(\\.([0-9]{1,2})?)?$"
var regex = try NSRegularExpression(pattern: expression, options: NSRegularExpressionCaseInsensitive)
var numberOfMatches = regex.numberOfMatches(inString: newString, options: [], range: NSRange(location: 0, length: newString.characters.count))
if numberOfMatches == 0 {
return false
}
}
}
catch let error {
}
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
let compSepByCharInSet = string.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet.joined(separator: "")
if string == numberFiltered {
let currentText = textField.text ?? ""
guard let stringRange = Range(range, in: currentText) else { return false }
let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
return updatedText.count <= 10
} else {
return false
}
}
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.