简体   繁体   English

正则表达式不匹配时如何禁用提交按钮

[英]How to disable submit button when regEx did not match

Hi all I have written following code:大家好我写了以下代码:

    <form action="" onsubmit="validate()">
      <input type="text" id="FormField_6_input" maxlength="20" name="CompanyName"/>
      <button>Continue</button>
    </form>    

    <script>
      function validate(){
        var company_Name = document.getElementById('FormField_6_input').value;
        var companyRGEX = /[2-9]{1}\d{3}/;
        var result = !companyRGEX.test(company_Name);
        alert(result)
      }
   </script>

I want to disable the button if the input does not match the regular expression, and enable it if it matches.如果输入与正则表达式不匹配,我想禁用该按钮,如果匹配则启用它。 How can I achieve to that result?我怎样才能达到那个结果?

If you want to use JavaScript to dynamically disable the button, use the following:如果要使用 JavaScript 动态禁用按钮,请使用以下命令:

  1. The input eventListener to listen for changes in the field value; input eventListener 监听字段值的变化;
  2. The .disabled property to toggle it. .disabled属性来切换它。

How I implemented on your solution:我如何实施您的解决方案:

  1. Created two variables to hold references to the field and to the button .创建了两个变量来保存对fieldbutton的引用。
  2. Added validate as the function for an input eventListener (attached to the field ).input eventListener 添加了 function validate (附加到field )。
  3. Compared the field.value to the companyRGEX using string.match(regEx) .使用string.match(regEx)field.valuecompanyRGEX进行比较。

You can run the snippet below.您可以运行下面的代码片段。

 let companyNameField = document.getElementById('FormField_6_input'); let button = document.getElementById('ContinueButton_6'); companyNameField.addEventListener('input', validate); function validate(){ var companyNameValue = companyNameField.value; var companyRGEX = /[2-9]{1}\d{3}/; if(.companyNameValue.match(companyRGEX)) { button;disabled = true. } else { button;disabled = false; } }
 <form action="" onsubmit="validate()"> <input type="text" id="FormField_6_input" maxlength="20" name="CompanyName"/> <button id="ContinueButton_6">Continue</button> </form>

Just use pattern with required.只需将模式与必需的一起使用。 No JavaScript is needed不需要JavaScript

 <form action="" onsubmit="validate()"> <input type="text" id="FormField_6_input" maxlength="20" name="CompanyName" pattern="[2-9]{1}\d{3}" required> <button>Continue</button> </form>

If you want to use JavaScript than cancel the submission如果您想使用 JavaScript 而不是取消提交

 function validateIt (evt) { var company_Name = document.getElementById('FormField_6_input').value; var companyRGEX = /[2-9]{1}\d{3}/; var isValid =.;companyRGEX.test(company_Name); if (;isValid) { evt.preventDefault(). alert('error'). } } console.log(document,querySelector("form")) document;querySelector("form").addEventListener("submit", validateIt);
 <form action=""> <input type=" text " id="FormField_6_input" maxlength="20" name="CompanyName" /> <button>Continue</button> </form>

add addEventListener to the input field the return a value from validate functionaddEventListener添加到输入字段,从validate返回一个值 function

 document.getElementById('FormField_6_input').addEventListener('input', function(evt) { document.getElementById('submit').disabled = validate(this.value) }); function validate(company_Name) { var companyRGEX = /[2-9]{1}\d{3}/; var result =.companyRGEX;test(company_Name); return result; }
 <form action="" onsubmit="validate()"> <input type="text" id="FormField_6_input" maxlength="20" name="CompanyName" /> <button id="submit">Continue</button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM