1

Hi i just working of Validation of UITextFiled For checking that it Starting with WhiteSpace. For Example I am Enter City name New York In UITextFild like Bellow Image:-

enter image description here

as above TextFiled Contain 6 blank space Before New York. so i want to check that User can not Enter Add white-space Before Word but it allow to Enterd black space of Sentence between two word.

I am using Bellow code for checking white-space.

NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSRange range =[txtCityName.text rangeOfCharacterFromSet:whitespace]; 

if(range.location != NSNotFound )
{
    NSLog(@"hi ==%@",Whitespace Found);

}
else
{
    NSLog(@"hi ==%@",Whitespace Not Found);
}

Above Code Check Full TextFiled Text contain white-space or not, But I want to check only Starting of Sentence and also Check After end Of sentence it contain white-space or not.

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • 1
    Can't you just correct the sentence after the user finished entering? You could then just take the complete string and delete all the white space before and after the sentence easily – HAS Mar 30 '13 at 09:26
  • you want to remove all the spaces which are placed before and after the string or just you want to know? – Balu Mar 30 '13 at 09:27
  • as par Pratik answer it's something Going Current. user can't enter whitespace befor starting sentence. so i am using this UITextfild method of delegate `shouldChangeCharactersInRange` and also `whitespaceAndNewlineCharacterSet` then my issue fixed. – Nitin Gohel Mar 30 '13 at 09:30

2 Answers2

2

just put below delegate method of textfiled. use below code

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

    if (range.location == 0 && [string isEqualToString:@" "]) {
        return NO;
    }
    return YES;
} 

try this user cannot insert space at starting of sentance

Pratik
  • 2,399
  • 17
  • 36
  • thank you Pratik for answered my question it working for check Starting of word whitespace but how to check after sentance of whitescpace – Nitin Gohel Mar 30 '13 at 09:04
1

You can check it with

 NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];

& if you want to remove it from your string , you need to work on following

 <NEW STRING> = [<STRING_CONTAINS WHITESPACE> stringByTrimmingCharactersInSet:whitespace];
 NSLog("String without white space = %@",NEW STRING);

Enjoy Programming!

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
  • thank's for reply Foram i know your puted Code it if i use this it remove all whitespace but i need only starting remove – Nitin Gohel Mar 30 '13 at 08:59
  • your are right and your Solution also usefull but as compere with Pratik answer it's much better user cant enter whitespace befor starting sentance and i am not accept your both answer so i just give you 1+ for helping me Foram Sorry :) – Nitin Gohel Mar 30 '13 at 09:33