So i want to validate the user ip during typing. In the VC i did the following :
extension NetworkSettingsViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.staticMask.resignFirstResponder()
self.staticGateway.resignFirstResponder()
self.staticIp.resignFirstResponder()
self.staticDns.resignFirstResponder()
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var isValidate: Bool
//verify deletion not happening
if !(range.length == 1) {
if validatorManager.verifyTarget(test: string) {
isValidate = true
} else {
isValidate = false
}
} else {
isValidate = true
}
return isValidate
}
}
This is the validation class :
class ValidatorManager: NSObject {
func verifyTarget(test: String) -> Bool {
// let validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
let validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){0,3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])?$"
let ipTest = NSPredicate(format:"SELF MATCHES %@", validIpAddressRegex)
print(ipTest.evaluate(with:test))
return ipTest.evaluate(with:test)
}
}
i have tried the 2 regex but nothing. i want to check char by char and then all the 3 before the dot() for all the octets.