我有一个HTML表单,我想在其中验证使用JavaScript提交的买方输入。
我想如果用户未输入字母,空格或数字,则将显示警报。基本上,我只接受字母,空格和数字。
为此,我将这个JS函数与买方字段的验证规则一起使用。但似乎是错误的:(
function validateForm () {
var amount = document.forms["salesform"]["amount"];
var buyer = document.forms["salesform"]["buyer"];
var receipt_id = document.forms["salesform"]["receipt_id"];
var buyer_email = document.forms["salesform"]["buyer_email"];
var note = document.forms["salesform"]["note"];
var city = document.forms["salesform"]["city"];
var phone = document.forms["salesform"]["phone"];
var entry_by = document.forms["salesform"]["entry_by"];
var buyerRegex = "/^[a-z][a-z\s]*$/"
if (amount.value == "") {
alert("Please enter the amount.");
amount.focus();
return false;
} else if (isNaN(amount.value)) {
alert("Amount should be only numeric value.");
amount.focus();
return false;
}
if (buyer.value == "") {
alert("Buyer name is required");
buyer.focus();
return false;
} else if (!buyer.value.match(buyerRegex)) {
alert("Buyer name only contain letter, number and space.");
buyer.focus();
return false;
}
return true;
}
答案 0 :(得分:-1)
您不应该使用0-9代替a-z吗?
var buyerRegex = "/^[a-z0-9\s]*$/"
答案 1 :(得分:-1)
已插入匹配项,尝试对regix使用测试
buyerRegex = "/^[a-z0-9\s]*$/"
if (!buyer.value.test(buyerRegex)) {
alert("Buyer name only contain letter, number and space.");
buyer.focus();
return false;
}