简体   繁体   English

如何在Angular JS中验证文本框

[英]How to validate text box in Angular js

I have a text box and Search Button in UI. 我在UI中有一个文本框和搜索按钮。 I want to validate it like only Numbers should be entered on button click. 我想验证它,就像单击按钮时只输入数字一样。

I didn't use it in Form(I got to know lot of ways to validate form control). 我没有在Form中使用它(我知道很多验证表单控件的方法)。 But simply i've designed it(independent) in my div. 但是简单地我已经在我的div中设计了它(独立)。

So can you pls guide me to validate my control in Angular. 因此,您能否指导我在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>

Use ngPattern directive. 使用ngPattern指令。

From documentation 从文档

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. 如果ngModel值与通过评估属性值中给出的Angular表达式找到的RegExp不匹配,则设置模式验证错误密钥。 If the expression evaluates to a RegExp object then this is used directly. 如果表达式的计算结果为RegExp对象,则可以直接使用它。 If the expression is a string then it will be converted to a RegExp after wrapping it in ^ and $ characters. 如果表达式是字符串,则将其用^和$字符包装后将转换为RegExp。 For instance, "abc" will be converted to new RegExp('^abc$'). 例如,“ abc”将转换为新的RegExp('^ abc $')。

<input type="text" ng-model="myVariable" ng-pattern="" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM