//要用stringByReplacingCharactersInRange把textField delegate的回傳值給拚起來
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var username = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if self.checkUsernameRule(username) {
self.submitButton.enabled = true
} else {
self.submitButton.enabled = false
}
// return NO to not change text
return true
}
func checkUsernameRule(username: String) -> Bool {
//Username should be 6–20 characters long, and start with a letter
println(username)
if username =~ "^[a-zA-Z]{1}(\\w{5,20})$" {
return true
}
return false
}
沒有留言:
張貼留言