为什么没有模式工作?

时间:2017-05-31 09:52:46

标签: javascript angularjs forms

在以下表单中,ng-pattern验证不起作用。 正则表达式正如我在https://regex101.com中所期望的那样工作。 如果用户输入一些特殊字符,它应显示.custom-error div。

我在哪里做错了?

<form novalidate name="myForm">
    <label for="subnet">only alphanumeric</label>
    <input type="text" name="subnet" ng-model="subnet" class="form-control"
           id="subnet" required ng-pattern="/[a-zA-Z\x7f-\xff]\s*/">
    <div class="custom-error" ng-show="myForm.subnet.$error.pattern">
        not in one of predefined characters
    </div>
</form>

2 个答案:

答案 0 :(得分:1)

我认为ng-show =“myForm.subnet。$ error.pattern”是错误的。我读了官方的角度文档,我认为你应该尝试ng-show =“!myForm.subnet。$ valid”而不是:

<input type="text" name="subnet" ng-model="subnet" class="form-control" id="subnet" required ng-pattern="/[a-zA-Z\x7f-\xff]\s*/">
<div class="custom-error" ng-show="!myForm.subnet.$valid">
   not in one of predefined characters
</div>

希望它能帮到你

答案 1 :(得分:-1)

将ng-pattern代码更改为function f() { cmd1 cmd2 } f | cmd3 。它现在可以工作了。

工作人员here

一切顺利。

相关问题