Saad Umar
Saad Umar

Reputation: 604

Objective c allow white space between two words only, not before nor after

I am working on an ios app using objective c, at one screen i have a name field, i want to validate the field in such a way that it only allow alphabetic name only with space between first and last name only, and no space before first part of name, similarly no space after last part of name in text field, i have done check for 'alphabetic name only' but stuck for this empty space part, plz. guide.

Upvotes: 2

Views: 832

Answers (1)

Fahim Parkar
Fahim Parkar

Reputation: 31657

How about this?

NSString *firstNameActual = firstNameTF.text;
NSString *firstNameTrimmed = [firstNameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([firstNameActual isEqualToString:firstNameTrimmed]) {
    // there is no space at start or end
} else {
    // there are spaces at start or end
}

Upvotes: 1

Related Questions