Hello,
I have a view has a textfield.
I want to limit input on it to only letters and put up an alert dialog if anything other than letters.
if anyone give me a good advice about it, I'd be very appreciated.
thanks,
c00012
Hello,
I have a view has a textfield.
I want to limit input on it to only letters and put up an alert dialog if anything other than letters.
if anyone give me a good advice about it, I'd be very appreciated.
thanks,
c00012
You shoals use the onChange on TextField to test on the fly. Do it in onSubmit to test only when you hit return key.
If the State var in the TextField is input, you should test if only letters
.onChange(of: input) { oldInput, proposedInput in
if onlyLetters(proposedInput) {
input = proposedInput
} else {
input = oldInput
}
}
onlyLetters is a func to test the proposed string contains only letters.
func onlyLetters(_ s: String) -> Bool {
return resultoftest
}
See here a way to implement: https://stackoverflow.com/questions/60657152/how-to-check-whether-string-only-consists-of-letters-and-spaces-in-swift-5