简体   繁体   English

如何使用正则表达式验证 Vue3 中的输入字段?

[英]How to validate an input field in Vue3 using Regex?

How is it possible to have custom input field validation using Regex?如何使用 Regex 进行自定义输入字段验证? I want input field to only have first 3 inputs as alphabets and last 3 inputs as numbers separated by "-".我希望输入字段只有前 3 个输入作为字母,后 3 个输入作为数字,用“-”分隔。 And I want to validate it same time use tries to type something.我想在使用尝试输入内容的同时验证它。

I found only the solutions to validate numbers but when I try to use the same code for alphabets and number it does not work.我只找到了验证数字的解决方案,但是当我尝试对字母和数字使用相同的代码时,它不起作用。 Also I wanted the input field to validate for each key stroke.此外,我希望输入字段验证每个击键。

 <template> <div class="mt-6"> <input type="text":placeholder="number_template" class="w-56 text-2xl bg-grat-300 p-3 rounded-lg focus:outline-none" v-model="number" /> <br /><br /> <input type="text":placeholder="reg_template" class="w-56 text-2xl bg-grat-300 p-3 rounded-lg focus:outline-none" v-model="number_plate" /> </div> </template> <script> export default { props: ['number_template', 'reg_template'], data: function() { return { number: '', number_format: '', // number pattern for now XXX-XXX regex: '', //regex for number pattern reg_regex: '', // regex for registration plate number reg_format: '', // pattern for registration number for now XXX-XXX (first 3 are letters and last 3 are numbers) number_plate: '' }; }, mounted() { let x = 1; this.format = this.number_template.replace(/X+/g, () => '$' + x++); console.log(this.format); this.number_template.match(/X+/g).forEach((char, key) => { this.regex += '(d{' + char.length + '})?'; console.log(this.regex); console.log(char.length); console.log(key); }); let y = 1; this.reg_format = this.reg_template.replace(/X+/g, () => '$' + y++); console.log(this.reg_format); this.reg_template.match(/X+/g).forEach((char, key) => { this.reg_regex += '(d{' + char.length + '})?'; console.log(this.reg_regex); console.log(char.length); console.log(key); }); }, watch: { number() { this.number = this.number.replace(/[^0-9]/g, '').replace(/^(\d{3})?(\d{3})/g, this.format).substr(0, this.number_template.length); }, number_plate() { this.number_plate = this.number_plate.replace(/([AZ]{3})?(d{3})/g, this.format).substr(0, this.reg_template.length); } } }; </script>

This code works for the first input field but doesn't work for the second one.此代码适用于第一个输入字段,但不适用于第二个输入字段。 I might need to change something in mounted function where its trying to match the reg_regex but I am not getting an idea how to do it.我可能需要更改已安装的 function 中的某些内容,它试图匹配 reg_regex 但我不知道该怎么做。

Browsers support this natively using the pattern attribute:浏览器使用pattern属性本机支持这一点:

<input
  type="text"
  :placeholder="reg_template"
  class="w-56 text-2xl bg-grat-300 p-3 rounded-lg focus:outline-none"
  v-model="number_plate"
  pattern="\w{3}-\d{3}"
/>

This will validate the form input client side as the user types.这将在用户输入时验证表单输入客户端。

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

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