javascript - 在 JavaScript 中创建正则表达式

标签 javascript html regex

我正在尝试进行表单验证,检查用户输入的输入是否与正则表达式匹配,然后表单应该通过。现在,我正在尝试验证名称字段,以便即使用户输入至少 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();"/>&nbsp;-&nbsp;<span id="firstNameAlert">First Name</span><br />
        <input name="quantityBottle" id="quantityBottle" type="text" value="0" size="20" maxlength="3" />&nbsp;Bottle Order Quantity<br />
        <input name="costBottle" id="costBottle" type="text" value="5.31" size="20" readonly="readonly" />&nbsp;Bottle Cost<br />
        <p class ="coloredBox" onclick="formCalculator();">Submit</span></p>
        <div id="result"></div>
     </form>
   </body>
</html>

最佳答案

量词与前面的标记匹配,该标记是当前正则表达式中的空格。因此,您的正则表达式匹配一个字母后跟 2-15 个空格。您想要使用字符集来匹配字母和空格(方括号)的任意组合。

/^[\w\s]{2,15}$/

参见:http://www.regular-expressions.info/charclass.html

关于javascript - 在 JavaScript 中创建正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716099/

相关文章:

javascript - child 没有根据 Vue.js 中的 props 进行更新

javascript - onLoad 到底何时发生

c# - 如何使用正则表达式从输入字符串中提取所有非字母数字字符?

html - 是否有 3d Canvas 的开源框架?

html - 如何在 CSS 和 HTML 中添加背景图片

Golang 中的正则表达式 : How to set character that makes the string not match?

javascript - 为什么 String.match(/\d*/) 返回空字符串?

javascript - 额外的小数点来自 Number.prototype.toFixed

javascript - 为什么 node.js 比 Google Chrome 慢得多?

html - 我可以停止 :hover from being applied to an element?