使用RegularExpressionValidator来设置正则表达式/^1\d{10}$/,该正则表达式表示用户名必须以1开头,后面跟着10位数字,总共11位。然后,在获取验证码按钮的点击事件中通过检查acceptableInput属性来验证手机号码的有效性。
手机账号输入框代码:
TextField {
id: accountTextField
Layout.alignment: Qt.AlignHCenter
implicitHeight: heightEdit
implicitWidth: widthEdit
placeholderText: qsTr("请输入11位手机号")
validator: RegularExpressionValidator { regularExpression: /^1\d{10}$/ }
selectByMouse: true
background: Rectangle{
border.color: "gray"
radius: 3
}
}
验证码按钮代码:
Text {
id: getCaptchaBtn
text: qsTr("获取验证码")
MouseArea {
anchors.fill: parent
onClicked: {
if (accountTextField.acceptableInput) {
console.log("手机号合法");
// 执行手机号合法时的逻辑
} else {
console.log("手机号不合法");
// 执行手机号不合法时的逻辑
}
}
}
}