javascript - 如何在 Angular js 中验证文本框

标签 javascript html angularjs angularjs-validation

我在用户界面中有一个文本框和搜索按钮。我想验证它,就像单击按钮时只应输入数字一样。

我没有在表单中使用它(我了解了很多验证表单控件的方法)。但我只是在我的 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/

相关文章:

javascript - 获取 URL 参数 elemId 并将其添加到按钮的 onClick 属性中

javascript - jquery 第二次事件没有响应

javascript - 使用正则表达式 Javascript 检索文本的父节点

angularjs - 将 Angular 服务注入(inject)集成测试

javascript - Angular 2注入(inject)全局依赖

javascript - 织物对象的不透明度 slider

javascript - Angular JS : Javascript not working inside custom directive template

javascript - Cookie 仅适用于 Firefox

javascript - 如何使用 AngularJS 指令解决 Jquery 焦点问题?

c# - 在节点和边列表中查找循环引用