Aanchal Aanchal - 1 year ago 61
AngularJS Question

Add validation for input field for either 7 or 9 digits in AngularJS

I am trying to add validation in my input field so that it can accept either 7 or 9 digits but I could not find any way to do this. In my code, I added

but it does not solve my purpose .

I want to add either 7 or 9 digits only so If I enter 8 digits in my box it should show an error. Below code I used but it does not fulfill my requirement-

<div class="form-group" ng-class="{ 'has-error' : myForm.num.$invalid && (myForm.$submitted || myForm.num.$touched ) || myForm.num.$error.minlength || myForm.num.$error.maxlength}">

<label for="UserName" style="color:#767676" class="" ng-hide="myForm.num.$invalid && (myForm.$submitted || myForm.num.$touched || myForm.num.$error.minlength || myForm.num.$error.maxlength)">Number</label>
<label class="error_message_text" ng-show="myForm.num.$invalid && (myForm.$submitted || myForm.num.$touched || myForm.num.$error.minlength || myForm.num.$error.maxlength)">
Please enter the number of either 7 or 9 digits
<input type="text" numbers-only name="num" class="form-control" ng-class="" ng-minlength="7" ng-maxlength="9" ng-model="user.name" ng-required='!user.memNo' required/>


I have created a plunker here-

Can anybody help me in my validations?

Answer Source


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download