我在用户界面中有一个文本框和搜索按钮。我想验证它,就像单击按钮时只应输入数字一样。
我没有在表单中使用它(我了解了很多验证表单控件的方法)。但我只是在我的 div 中设计了它(独立)。
您能指导我验证我在 Angular 中的控制吗?
<div id="srchbox">
<p id="ptext">Please enter the Movie Id</p>
<input type="text" class="form-control" placeholder="Search" id="Srchtxt" ng-model="_Id">
<button type="submit" class="btn btn-default" id="btnsrch" ng-click="search(_Id)"><span class="glyphicon glyphicon-search"></span></button>
</div>
最佳答案
使用ngPattern
指令。
来自文档
Sets pattern validation error key if the ngModel value does not match a RegExp found by evaluating the Angular expression given in the attribute value. If the expression evaluates to a RegExp object then this is used directly. If the expression is a string then it will be converted to a RegExp after wrapping it in ^ and $ characters. For instance, "abc" will be converted to new RegExp('^abc$').
<input type="text" ng-model="myVariable" ng-pattern="" />
关于javascript - 如何在 Angular js 中验证文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29393170/