0

I'm making a simple app in which there is an NSTextField and I want only alpha-numeric characters in it while typing.

Can anyone suggest an approach to this please?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
JNX
  • 29
  • 2
  • The solution seems to require subclassing; see: http://stackoverflow.com/questions/4652689/restrict-nstextfield-input-to-numeric-only-nsnumberformatter – trojanfoe Apr 29 '14 at 06:36
  • possible duplicate of [allow only alphanumeric characters for a UITextField](http://stackoverflow.com/questions/7541803/allow-only-alphanumeric-characters-for-a-uitextfield) – Rahul Patel Apr 29 '14 at 07:05
  • 1
    @RahulPatel The question is about `NSTextField`, not `UITextField`. – trojanfoe Apr 29 '14 at 07:13

4 Answers4

2

There's a built-in support for that.

  • In Interface Builder check "Only Roman Characters" option for the text field.

OR

  • In your code set this property:

    [myTextField.cell setAllowedInputSourceLocales: @[NSAllRomanInputSourcesLocaleIdentifier]];
    
pointum
  • 2,987
  • 24
  • 31
  • 1
    pointum this one seems to be the best way but this is not working :( used this code when i created the text field still it accepts special chars :'( – JNX Apr 29 '14 at 09:18
-2
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
    if ([self validCharecter:textField.text){

      return YES;
  }
 else{

      return NO:
   }
   }

    -(BOOL)validCharecter :(NSString)textStr{

        NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
        BOOL validStr = [[textStr stringByTrimmingCharactersInSet:alphaSet]  isEqualToString:@""];

        return validStr;
  }

Try with dis hope it will helps you!!

simbesi.com
  • 1,539
  • 3
  • 17
  • 27
-2

Take a look at below code sample. I hope this will help you.

 //#define CHARACTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

 //#define CHARACTERS_NUMBERS  [CHARACTERS stringByAppendingString:@"1234567890"]

///// Inside shouldChangeCharactersInRange

///////////>>>>>>>>>>>>>>>>>>

if(textField== txtFldAlpha)
    {

        //Alpha only

        NSUInteger newLength = [textField.text length] + [string length] - range.length;

        NSCharacterSet *unacceptedInput =
        [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];

        // Create array of strings from incoming string using the unacceptable
        // characters as the trigger of where to split the string.
        // If array has more than one entry, there was at least one unacceptable character
        if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
            return NO;
        else 
            return YES&&(newLength < 26);
        return YES;
    }

///////////<<<<<<<<<<<<<<<<<<

///////////>>>>>>>>>>>>>>>>>>

if(textField==txtFldNumeric)
    {
        //Num only

        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

        if ([[string componentsSeparatedByCharactersInSet:nonNumberSet] count] > 1)
            return NO;
        else 
            return YES&&(newLength < 6);
        return YES;

    }

///////////<<<<<<<<<<<<<<<<<<

///////////>>>>>>>>>>>>>>>>>>

if(textField==txtFieldNumAlphaSpecial)
    {
        //Num,Alpha,Special field

        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        return (newLength > 50) ? NO : YES;
    } 

///////////<<<<<<<<<<<<<<<<<<

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
iLearner
  • 1,670
  • 2
  • 19
  • 45
  • Can you please mention where `shouldChangeCharactersInRange` method is declared? Note that the question is about Cocoa-NSTextField. Nothing related to iOS. – Sunil Chauhan Jun 20 '17 at 14:59
-3
#define ACCEPTABLE_CHARACTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
  NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];

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

  return [string isEqualToString:filtered];
}

OR Implement

textField:shouldChangeCharactersInRange:replacementString:

in the delegate, check the replacement string for special characters, and disallow the replacement if you detect any.

The easiest way to check for non-alphanumerics is as follows:

if ([replacementString rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
// There are non-alphanumeric characters in the replacement string
}

Hope this helps mate .. !

iAhmed
  • 6,556
  • 2
  • 25
  • 31