ios - Restricting text input to only alpha characters -


i have seen multiple approaches cannot work.

i trying restrict text field allow alpha characters entered it. i.e. abcdefabcdef (but of them).

here existing method:

-(bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string {      // check space/delete     if (string.length <=0 ) {         if ([self.wordarray lastobject]) {             [self.wordarray removeobjectsinrange:range];             [self.tilecollectionview reloaddata];             return yes;          }     }      // check make sure word not above 16 characters, should enough right?     if (textfield.text.length >= 16 ) {         nslog(@"wooo slow down text above 16");         return no;     } else {             [self.wordarray addobject:string];             [self.tilecollectionview reloaddata];             return yes;     }  } 

at present check space , remove last entry array. if letter accepted add letter object array, else. logic alpha check should take account, if letter 'legal' should add array , reload collection view.

well, 1 way create own character set compare against. can take advantage of nsstring's stringbytrimmingcharactersinset: , nscharacterset's invertedset property remove characters set don't match characters specify. then, if final string matches input string, didn't contain illegal characters.

nscharacterset *mycharset = [nscharacterset charactersetwithcharactersinstring:@"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"];  nsstring *input = @"a";  nsstring *output = [input stringbytrimmingcharactersinset:[mycharset invertedset]];  bool isvalid = [input isequaltostring:output];  nslog(@"%d",isvalid); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -