ios - Automaticlly insert '/' in date field -


i have uitextfield format: dd/mm/yyyy, example: 12/03/1982 wanna when user type "2" , '3', field insert '/' after them automatically. performed in method:

-(bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string 

with check:

nsstring *wholetextname = [textfield.text stringbyreplacingcharactersinrange:range                                         withstring:string]; logdebug(@"wholetext: %@", wholetextname);       if ([textfield isequal:self.dateofbirthtextfield])     {         if (([wholetextname length] == 2)||(([wholetextname length] == 5))) {         textfield.text=[wholetextname stringbyappendingstring:@"/"];         } 

but when i'm typing '2', result on textfield is: '12/2' not '12/'=expected result u suggest me please?

use below code implement desired result or textfield validation.

-(bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string {     nsstring *wholestring = textfield.text;     if ((wholestring.length == 2 || wholestring.length == 5) && ![string isequaltostring:@""])     {        wholestring = [wholestring stringbyappendingstring:@"/"];        textfield.text = wholestring;     }     else if(wholestring.length == 10 && ![string isequaltostring:@""])     {        return no;     }      return yes; } 

here have added else if condition restrict more 10 characters , 1 more condition ![string isequaltostring:@""], if enter backspace, work fine.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -