gpt4 book ai didi

iOS - 在输入过程中验证用户 ip 地址

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:03 24 4
gpt4 key购买 nike

所以我想在打字时验证用户 ip。在 VC 中,我做了以下操作:

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
}

}

这是验证类:

 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)
}
}

我已经尝试了 2 个正则表达式,但没有。我想逐个字符检查一个字符,然后检查所有八位字节的 dot() 之前的所有 3 个字符。

最佳答案

这里有两个功能:第一个检查用户输入的内容是否有效(未完成的 IP),第二个检查整个内容:

func verifyWhileTyping(test: String) -> Bool {
let pattern_1 = "^(([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 regexText_1 = NSPredicate(format: "SELF MATCHES %@", pattern_1)
let result_1 = regexText_1.evaluate(with: test)
return result_1
}

func verifyWholeIP(test: String) -> Bool {
let pattern_2 = "(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})"
let regexText_2 = NSPredicate(format: "SELF MATCHES %@", pattern_2)
let result_2 = regexText_2.evaluate(with: test)
return result_2
}

textField(_ textField: , shouldChangeCharactersIn range:, replacementString string:) 中使用 verifyWhileTyping(test:) 来在输入时进行检查。当用户完成并单击按钮或按下 Enter 键时调用 verifyWholeIP(test:):

//While typing
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
verifyWhileTyping(test: text + string)
}
//...
}

//When Enter is tapped
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
if let text = textField.text {
verifyWholeIP(test: text)
}
//...
return true
}

pattern_1 在用户输入时检查它是否是正确 IP 的开头:

regexText_1.evaluate(with: "0")        //true
regexText_1.evaluate(with: "255") //true
regexText_1.evaluate(with: "256") //false
regexText_1.evaluate(with: "10.10.") //true
regexText_1.evaluate(with: "1.2..") //false
regexText_1.evaluate(with: "1.2.3.4") //true
regexText_1.evaluate(with: "1.2.3.4.") //false

至于pattern_2,它评估整个IPv4:

"(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})"

这里有更多的测试用例:

regexText_2.evaluate(with: "0.0.0.0")    //true
regexText_2.evaluate(with: "1.1.1.256") //false
regexText_2.evaluate(with: "-1.0.1.2") //false
regexText_2.evaluate(with: "12.34.56") //false
regexText_2.evaluate(with: "I.am.an.IP") //false

对于IPv6,这是要使用的正则表达式:[0-9A-Fa-f]{1,4}" in pattern_2 .

关于iOS - 在输入过程中验证用户 ip 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com