Reputation: 297
so i make a form that contain a bunch of data input from the user, on of the input is age, and i want the user to only input between 17 - 100.
after that i have this func that contain the validation of every textfield, so if the validation is correct it will make the button to enabled
this is my validation func :
func buttonReady() {
if umurTextField.text != "" && domisiliTextField.text != "" && rencanaPembelianTextField.text != "" && luasTanahTextField.text != "" && luasBangunanTextField.text != "" && budgetTextField.text != "" && metodePembayaranTextField.text != "" && lantaiRumahTextField.text != "" {
ButtonSearch.isEnabled = true
ButtonSearch.backgroundColor = #colorLiteral(red: 0.5176470588, green: 0.3294117647, blue: 0.1333333333, alpha: 1)
ButtonSearch.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: .normal)
ButtonSearch.layer.cornerRadius = 25
}else {
ButtonSearch.isEnabled = false
ButtonSearch.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
ButtonSearch.layer.cornerRadius = 25
ButtonSearch.layer.backgroundColor = #colorLiteral(red: 0.6666666667, green: 0.4745098039, blue: 0.2588235294, alpha: 0.4408410685)
}
}
so far i just make the validation into not empty, but i want to make that "umurTextField" validation into only between 17 - 100, how do i do that?
thanks
Upvotes: 0
Views: 174
Reputation: 51973
You can test if it is in the range using
17...100 ~= Int(umurTextField.text) ?? .min
Example
for test in ["8", "34", "b", "99", "104"] {
print("\(test): \(17...100 ~= Int(test) ?? .min)")
}
8: false
34: true
b: false
99: true
104: false
Upvotes: 2
Reputation: 100503
Could be
if let res = umurTextField.text ,
let value = Int(res),
(17...100).contains(value) , .......... {
}
Upvotes: 1