Mohd Sadham
Mohd Sadham

Reputation: 435

Avoid symbols except some which I specified

Possible Duplicate: Separate digits in an NSString to display a phone number

Objective:

How can i do it? Please reply with which members/property/delegates/interface helpful to these objective. More than to send whole code.

Upvotes: 0

Views: 113

Answers (5)

GenieWanted
GenieWanted

Reputation: 4513

Shouldn't this simply be like this:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.tag == 3)
    {

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."]; //This allows user type in only numbers and - +
        for (int i = 0; i < [string length]; i++)
        {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c]) //Here, it checks if the entered character is in the characterSet, if it's not, it wouldn't allow the user go ahead.
            {
                return NO; //Restrict the user from entering other characters
            }
        }

        return YES; //Allow the user enter the characters specified above
    }

    return YES; 
}

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49710

You can using NSCharacterSet in to one of the delegate of textFiled - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string using Bellow code:-

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

    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789-,+"] invertedSet];

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


    NSLog(@"%@",filtered);

  if(filtered.length>0)
  {
      //do your stuff
  }
  else
  {
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Not allowed" message:@"You Enter not allowd charactore" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];

  }
    return [string isEqualToString:filtered];
}

NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789*#"] invertedSet];user can able to enter only mention characters 0123456789-,+ in to textfiled.

Upvotes: 2

Suyog Patil
Suyog Patil

Reputation: 1620

Declare the number set as

#define NUMBERS @"0123456789+-"

Then in the delegate function of the text field

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
            NSCharacterSet *cs;
            NSString *filtered;
            cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
            filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return [string isEqualToString:filtered];"
}

I hope it will work for you

Upvotes: 1

KudoCC
KudoCC

Reputation: 6952

There is an delegate of UITextField named UITextFieldDelegate

Implement the method:

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

return NO if string contains invalid characters and give alert to the user.

How to judge if there is some invalid characters in the string, here is an example

NSCharacterSet * invalidCharacters = [NSCharacterSet characterSetWithCharactersInString:@"+-1234567890"] ;
invalidCharacters = [invalidCharacters invertedSet] ;
NSRange r = [searchString rangeOfCharacterFromSet:invalidCharacters] ;
if (r.location == NSNotFound) {
    // there isn't invalid character
} else {
    // there is
}

Upvotes: 0

Maddy B
Maddy B

Reputation: 51

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

in string you will find the character according to it, you can display what ever you want.

Upvotes: 0

Related Questions