Javascript 为什么';什么样的工作?
在下表中,Javascript 为什么';什么样的工作?,javascript,angularjs,forms,Javascript,Angularjs,Forms,在下表中,ng模式验证不起作用。 正则表达式在https://regex101.com。 如果用户输入了一些特殊字符,它应该显示.customerrordiv 我哪里做错了 <form novalidate name="myForm"> <label for="subnet">only alphanumeric</label> <input type="text" name="subnet" ng-model="subnet" class
ng模式
验证不起作用。
正则表达式在https://regex101.com
。
如果用户输入了一些特殊字符,它应该显示.customerror
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>
只有字母数字
不是在预定义的字符中
我认为ng show=“myForm.subnet.$error.pattern”是错误的。我阅读了官方的angular文档,我认为您应该尝试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>
不是在预定义的字符中
它希望它能帮助您将ng模式代码更改为
ng模式=“/^[a-zA-Z\x7f-\xff\s]*$/”
。现在可以了
工作打捞器
最好。OP不想在出现问题时显示该消息,而不是在消息有效时显示该消息吗?没错,只需使用ng show=“!myForm.subnet.$valid”代替=)如果用户输入特殊字符,如@,则带有自定义错误的div应该可见。但事实并非如此。在you plunk Too中,我刚刚更新了plunk。一旦看到它,普朗克就不行了。。它说不是所有输入的有效字符我刚刚更新了plunker。一旦看到它。我刚刚纠正了他的错误代码。他应该编写正确的正则表达式来实现这一点。无论如何,我更新了它。这确实有效,但在我的情况下不行,因为表单也接受了一些国际字符,比如äöü@Fawad,我更新了plunker。现在它可以满足您的所有要求。检查一下。