话不多少,直接附上代码实例,仅供参考
function validation() {
var val = document.getElementById("txtNumber").value;
var regu = /^(-|+)?d+(.d+)?$/;
if (val != "") {
if (!regu.test(val)) {
document.getElementById("labResult").style.color = "red";
document.getElementById("labResult").innerHTML = "验证失败!";
} else {
document.getElementById("labResult").style.color = "green";
document.getElementById("labResult").innerHTML = "验证成功!";
}
}
}
验证结果: