user3436410
user3436410

Reputation: 3

How to validate text box in Angular js

I have a text box and Search Button in 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). But simply i've designed it(independent) in my div.

So can you pls guide me to validate my control in 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>

Upvotes: 0

Views: 663

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27023

Use ngPattern directive.

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. 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="" />

Upvotes: 2

Related Questions