What I want to do is set some boolean in the if statement below to true if anything other than letters, numbers, dashes and spaces is contained in the textField.
我要做的是将下面if语句中的一些布尔值设置为true,如果textField中包含除字母、数字、破折号和空格之外的任何内容。
Then for an email field check for a valid email.
然后在电子邮件字段中检查有效的电子邮件。
This is how I'm checking for length:
这就是我检查长度的方法:
if countElements(textField.text!) < 2 || countElements(textField.text) > 15 {
errorLabel.text = "Name must have between 2 and 15 characters."
self.signUpView.signUpButton.enabled = false
} else {
errorLabel.text = ""
self.signUpView.signUpButton.enabled = true
}
This if statement is inside the UITextFieldDelegate method textFielddidEndEditing. I have a feeling I'll have to use some form of regex to check for a valid email.
这个if语句位于UITextFieldDelegate方法textFielddidEndEditing中。我有一种感觉,我必须使用某种形式的regex来检查有效的电子邮件。
Would it make sense to use a regex to check a field only contains the characters I allow and return a boolean that'll tell me if the text field contains disallowed characters so I can then show an error message.
使用regex检查字段是否只包含我允许的字符,并返回一个布尔值,该布尔值将告诉我文本字段是否包含不允许的字符,以便我可以显示错误消息。
Is there some preferred way to do this? In objective-c I used NSCharacterSet but not sure how to implement that here.
有什么更好的方法吗?在objective-c中,我使用了ns字符集但不确定如何在这里实现。
Thanks for your time
谢谢你的时间
1 个解决方案
#1
1
This is how I done it in the end.
最后我就是这么做的。
override func textFieldDidEndEditing(textField: UITextField) {
let usernameField = self.signUpView.usernameField.text as NSString
let alphaNumericSet = NSCharacterSet.alphanumericCharacterSet()
let invalidCharacterSet = alphaNumericSet.invertedSet
let rangeOfInvalidCharacters = usernameField.rangeOfCharacterFromSet(invalidCharacterSet)
let userHasNameInvalidChars = rangeOfInvalidCharacters.location != NSNotFound
if textField == self.signUpView.usernameField {
if userHasNameInvalidChars {
errorLabel.text = "Letters and numbers only please!"
self.signUpView.signUpButton.enabled = false
// do more error stuff here
} else {
self.signUpView.signUpButton.enabled = true
}
}
}
Thanks to the blog post: http://toddgrooms.com/2014/06/18/unintuitive-swift/
谢谢你的博客:http://toddgrooms.com/2014/06/18/un- swift/。
#1
1
This is how I done it in the end.
最后我就是这么做的。
override func textFieldDidEndEditing(textField: UITextField) {
let usernameField = self.signUpView.usernameField.text as NSString
let alphaNumericSet = NSCharacterSet.alphanumericCharacterSet()
let invalidCharacterSet = alphaNumericSet.invertedSet
let rangeOfInvalidCharacters = usernameField.rangeOfCharacterFromSet(invalidCharacterSet)
let userHasNameInvalidChars = rangeOfInvalidCharacters.location != NSNotFound
if textField == self.signUpView.usernameField {
if userHasNameInvalidChars {
errorLabel.text = "Letters and numbers only please!"
self.signUpView.signUpButton.enabled = false
// do more error stuff here
} else {
self.signUpView.signUpButton.enabled = true
}
}
}
Thanks to the blog post: http://toddgrooms.com/2014/06/18/unintuitive-swift/
谢谢你的博客:http://toddgrooms.com/2014/06/18/un- swift/。