修改网络设置校验提示策略。

This commit is contained in:
luocai
2024-08-22 12:28:11 +08:00
parent 7e28896159
commit eeb1fc088c
5 changed files with 63 additions and 34 deletions

View File

@ -6,22 +6,13 @@ Column {
id: root
property alias text: input.text
property bool valid: false
property bool canEmpty: false
TextField {
height: 36
width: 350
id: input
onTextChanged: {
if(text.length<=0){
hint.text = "参数不能为空"
return
}
valid = validateIp(text)
if (!valid) {
hint.text = "参数配置无效"
} else {
hint.text = ""
}
validate()
}
}
Text {
@ -30,12 +21,22 @@ Column {
font.pixelSize: 12
}
function validateIp(ip) {
// Regular expression for validating IP address
var regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/
return regex.test(ip)
}
function reset() {
hint.text = ""
}
function validate() {
if (input.text.length <= 0) {
hint.text = root.canEmpty ? "" : "参数不能为空"
root.valid = root.canEmpty
return root.valid
}
var regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/
root.valid = regex.test(input.text)
if (!root.valid) {
hint.text = "参数配置无效"
} else {
hint.text = ""
}
return root.valid
}
}

View File

@ -8,6 +8,7 @@ Popup {
parent: Overlay.overlay
anchors.centerIn: Overlay.overlay
modal: true
closePolicy: Popup.CloseOnEscap | Popup.NoAutoClose
property int inputHeight: 50
background: Rectangle {
radius: 8
@ -40,11 +41,17 @@ Popup {
Row {
spacing: 5
visible: staticMode.checked
Label {
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: -10
text: "设备IP"
Row {
width: 100
anchors.verticalCenterOffset: -10
anchors.verticalCenter: parent.verticalCenter
Label {
text: "设备IP"
}
Label {
color: "red"
text: "*"
}
}
IpTextField {
@ -58,11 +65,17 @@ Popup {
Row {
spacing: 5
visible: staticMode.checked
Label {
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: -10
Row {
width: 100
text: "子网掩码"
anchors.verticalCenterOffset: -10
anchors.verticalCenter: parent.verticalCenter
Label {
text: "子网掩码"
}
Label {
color: "red"
text: "*"
}
}
IpTextField {
@ -88,12 +101,13 @@ Popup {
width: 350
height: inputHeight
text: App.currentNetworkInfomation.gateway
canEmpty: true
}
}
Row {
spacing: 5
visible: staticMode.checked
visible: false // staticMode.checked 暂时不用设置
Label {
anchors.verticalCenter: parent.verticalCenter
text: "DNS服务器"
@ -106,6 +120,7 @@ Popup {
width: 350
height: inputHeight
text: App.currentNetworkInfomation.dns
canEmpty: true
}
}
@ -117,6 +132,10 @@ Popup {
Button {
text: "保存"
onClicked: {
ipInput.validate()
netmaskInput.validate()
gatewayInput.validate()
dnsInput.validate()
if (dhcpMode.checked || (staticMode.checked && ipInput.valid
&& netmaskInput.valid
&& gatewayInput.valid
@ -124,10 +143,9 @@ Popup {
App.updateNetworkInfomation(dhcpMode.checked,
ipInput.text,
netmaskInput.text,
gatewayInput.text,dnsInput.text)
gatewayInput.text,
dnsInput.text)
networkPopup.close()
} else {
showMessageDialog(2, "网络设置", "请输入合法参数地址!")
}
}
}
@ -139,9 +157,18 @@ Popup {
}
}
onVisibleChanged: {
ipInput.reset()
netmaskInput.reset()
gatewayInput.reset()
dnsInput.reset()
if (visible) {
ipInput.reset()
netmaskInput.reset()
gatewayInput.reset()
dnsInput.reset()
dhcpMode.checked = App.currentNetworkInfomation.dhcp
staticMode.checked = !App.currentNetworkInfomation.dhcp
ipInput.text = App.currentNetworkInfomation.ip
netmaskInput.text = App.currentNetworkInfomation.netmask
gatewayInput.text = App.currentNetworkInfomation.gateway
dnsInput.text = App.currentNetworkInfomation.dns
}
}
}