Вопрос

I am constructing a form and have managed to sort out the input in the form. What I am attempting to do is when an entry is inputed for the postcode it does a check to make sure it follows a pre determined format. So far I have removed the space, made 2 character sets, one with numerical values and one with just letters. And then done a check on the string length. This is below:-

NSCharacterSet *cset = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
NSCharacterSet *nset = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];

_QuestionAnswer.text = [_QuestionAnswer.text stringByReplacingOccurrencesOfString:@" " withString:@""]; //removes the space character
if (4 < _QuestionAnswer.text.length < 8) //checks string length between 5 and 7
{
    // this is where the format check should be done using cset and nset
}

NB UK post codes must follow any one of the example formats below:-

AA9A 9AA,

A9A 9AA,

A9 9AA,

A99 9AA,

AA9 9AA,

AA99 9AA,

Where 'A' is from cset and '9' is from nset

Thank you in advance.

Это было полезно?

Решение

You can use regular expressions (my is just example):

NSString *laxString = @".+@([A-Za-z0-9]+\\.)";
NSPredicate *postCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", laxString];

return [postCode evaluateWithObject: postCodeTest];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top