0

我正在构建一个表单并设法整理出表单中的输入。我试图做的是,当为邮政编码输入一个条目时,它会检查以确保它遵循预先确定的格式。到目前为止,我已经删除了空格,制作了 2 个字符集,一个带有数值,一个只有字母。然后检查字符串长度。这是下面的: -

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 邮政编码必须遵循以下任何一种示例格式:-

AA9A 9AA,

A9A 9AA,

A9 9AA,

A99 9AA,

AA9 9AA,

AA99 9AA,

其中'A'来自cset,'9'来自nset

先感谢您。

4

1 回答 1

1

您可以使用正则表达式(我的只是示例):

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

return [postCode evaluateWithObject: postCodeTest];
于 2013-11-06T11:29:27.830 回答