[英]Creating a regular expression in javascript
I'm trying to do a form validation where we check if the input the user enters matches a regular expression, than the form should go through. 我正在尝试进行表单验证,在该过程中,我们检查用户输入的输入是否与正则表达式匹配,而不是表单应该通过的表达式。 For now I'm trying to validate the name field so that even if the users enters a minimum of 2-15 characters spaces can be included.
现在,我正在尝试验证名称字段,以便即使用户输入了至少2-15个字符,也可以包含空格。 Shouldn't it be /^\\w\\s{2,15}$/;
应该不是/ ^ \\ w \\ s {2,15} $ /;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bottles Form</title>
<link rel="stylesheet" type="text/css" href="./css/form.css">
<script type="text/javascript">
function validFirstName() {
var fName = document.getElementById("customerName").value;
var regex = /^\w{2,15}$/;
if (regex.test(fName)) {
document.getElementById("firstNameAlert").innerHTML = "<span class='valid'>Valid</span>";
return (true);
} else {
document.getElementById("firstNameAlert").innerHTML = "<span class='error'>Error. First Name must be within 2 to 15 characters</span>";
return (false);
}
}
function formCalculator() {
const SALESTAX = .0825;
var userName = document.getElementById("customerName").value;
var quantity = document.getElementById("quantityBottle").value;
var costBottle = document.getElementById("costBottle").value;
var totalBottle = quantity * costBottle;
var discount = 0;
var discountedTotal = 0;
var taxTotal = totalBottle * SALESTAX;
var orderTotal = totalBottle + taxTotal;
//var orderWithDiscount = discountedTotal + taxTotal;
if(parseInt(quantity) > 10 && parseInt(quantity) <= 19) {
discount = .10;
totalBottle = totalBottle - (totalBottle * discount);
}
orderTotal.value = "$" + orderTotal.toFixed(2);
document.getElementById("result").innerHTML = "Hello " + userName + " - Your order of " + quantity + " bottles, costs $" + orderTotal.toFixed(2) + ", plus tax.";
}
</script>
</head>
<body>
<h1>Bottles Form</h1>
<form action="" method="post" enctype="multipart/form-data" name="wigetCalc">
<input name="customerName" id="customerName" type="text" size="20" onblur="validFirstName();"/> - <span id="firstNameAlert">First Name</span><br />
<input name="quantityBottle" id="quantityBottle" type="text" value="0" size="20" maxlength="3" /> Bottle Order Quantity<br />
<input name="costBottle" id="costBottle" type="text" value="5.31" size="20" readonly="readonly" /> Bottle Cost<br />
<p class ="coloredBox" onclick="formCalculator();">Submit</span></p>
<div id="result"></div>
</form>
</body>
</html>
Quantifiers match the preceding token, which is a space in your current regex. 量词与前面的标记匹配,该标记是当前正则表达式中的空格。 So your regex matches one letter followed by 2-15 spaces.
因此,您的正则表达式匹配一个字母,后跟2-15个空格。 You want to use a character set to match any combination of letters and spaces (square brackets).
您想使用字符集来匹配字母和空格(方括号)的任何组合。
/^[\w\s]{2,15}$/
See: http://www.regular-expressions.info/charclass.html 请参阅: http : //www.regular-expressions.info/charclass.html
Why not just use length? 为什么不只使用长度?
var fName = document.getElementById("customerName").value;
if (fName.length >= 2 && fName.length <= 15) {
document.getElementById("firstNameAlert").innerHTML = "<span class='valid'>Valid</span>";
} else {
document.getElementById("firstNameAlert").innerHTML = "<span class='error'>Error. First Name must be within 2 to 15 characters</span>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.