【问题标题】:vue.js see if input matches regexvue.js 查看输入是否匹配正则表达式
【发布时间】:2019-11-27 16:11:38
【问题描述】:

我有一个输入字段,我喜欢将其与几个正则表达式进行比较。 如果输入正确,则应该执行该方法。

我认为我做错了。

<p class="control">
    <input ref="barcode" id="BarcodeInput" v-model="barcode" v-on:keyup.enter="processBarcode" class="input" type="text"  placeholder="Barcode" autofocus />
    </p>
    <p class="control">
    <button v-on:click="processBarcode" class="button is-primary" >Go!</button>
</p>

方法:

      if (document.getElementById("BarcodeInput").value == "31334866-001"){

现在我可以将条形码放在文本框中,我可以使用方法了。

如果我尝试:

      if (document.getElementById("BarcodeInput").value == "^\d{8}-\d{3}$"){

条形码:31334866-001 不起作用。

【问题讨论】:

  • 你可以像if(/^\d{8}-\d{3}$/.test(document.getElementById("BarcodeInput").value)) {}一样使用test

标签: regex vue.js compare


【解决方案1】:

因此,您需要首先使用v-model 作为链接到您的data 的条形码输入。这将为您省去获取元素的麻烦。然后在processBarcode 函数中进行验证。

如果要清除框,只需清除数据中存储的v-model变量即可(使用双向绑定,改变一个改变另一个)。

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 2012-12-16
    • 2019-07-13
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多