-2

I am new to iOS.

I have created a UITextField and I want to give validations for phone number so that only numbers must be allowed. If any character or other than numbers is entered it must not be accepted, like it must not be visible in textField.

Thanks in advance.

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
user3291536
  • 29
  • 1
  • 1
  • 5
  • 1
    Are you looking for a way to remove non-numeric digits, for showing a numeric keyboard and/or for enforcing a valid phone number to be typed (and formatting it consequently)? – ItalyPaleAle Feb 10 '14 at 05:33

2 Answers2

1

You can set textfield keyboard type is Phone pad

or

Make a macro

#define ACCEPTABLE_CHARECTERS @"0123456789."

And use it

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

if (textField==textFieldAmount)
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet];

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    return [string isEqualToString:filtered];
}
return YES;
}
Kalpesh
  • 5,336
  • 26
  • 41
0

@user3291536, Better to try or search first, then post your query. Anyway follow these links,

Validating phone number in uitextfield

UITextField for Phone Number

how to perform validation on textfield for phone number entered by user in iphone

Community
  • 1
  • 1
Laxmi
  • 132
  • 1
  • 3
  • 12