-2
<label>Phone Number:</label>
     <input type="number" name="mobileNo" ng-minlength="10" ng-maxlength="10" required/>

I have problem with this I am trying a lot to make the entered phone number validation on clicking submit button and I was not able to validate the phone number.

The previous questions are showing weather the number is valid on not as we start entering the number but they are not validating on clicking the submit action button. Even I tried those examples and this is similar but not duplicate.

Thanks if any help in advance

2
  • Use ngPattern or custom directive. Both way are quite simple the first one might be simpler though, depends on your needs. Commented Feb 21, 2016 at 11:07
  • And don't forgot to have an ng-model on the field Commented Feb 21, 2016 at 11:09

1 Answer 1

0

You can try this:

<div ng-app ng-controller="formCtrl">
  <form name="myForm" ng-submit="onSubmit()">
   <input type="number" ng-model="mobile_number" name="mobile_number" ng-pattern="/^^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$/" required>
   <span ng-show="myForm.mobile_number.$error.pattern">
   Not a valid number!
   </span>
   <input type="submit" value="submit"/>
  </form>
</div>

JS :-

function formCtrl($scope){
 $scope.onSubmit = function(){
 alert("form submitted");
 }
}

Check out here : https://plnkr.co/edit/LzKbnLkECps6DwjW6gtH?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

ng-pattern="/^[0-9]{1,7}$/" how it works ?
It is regular expression pattern.I updated it for better performance. For more information you can check here : docs.angularjs.org/api/ng/directive/ngPattern

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.