56

I use ng-pattern="/0-9/" to set price_field do not accept decimal number. But when I input natural number (from 0 to 9999999),ng-show gets activated with Not valid number!.

Where did I go wrong?. Please help.

<form name="myform" data-ng-submit="create()">
        <input type="number"
               name="price_field"
               data-ng-model="price"
               require
               ng-pattern="/0-9/">
        <span  ng-show="myform.price_field.$error.pattern">Not valid number!</span>
        <input type="submit" class="btn">
</form>
Stanislav
  • 4,389
  • 2
  • 33
  • 35
user2888686
  • 667
  • 2
  • 9
  • 12

3 Answers3

126

The problem is that your REGX pattern will only match the input "0-9".

To meet your requirement (0-9999999), you should rewrite your regx pattern:

ng-pattern="/^[0-9]{1,7}$/"

My example:

HTML:

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

JS:

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

Here is a jsFiddle demo.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Chickenrice
  • 5,727
  • 2
  • 22
  • 21
  • According to the documentation (https://docs.angularjs.org/api/ng/directive/input), you could also write: ng-pattern="[0-9]{1,7}", which is a little bit more concise (and Angular already makes the HTML dirty enough, I think). – DanielM Aug 25 '15 at 08:24
  • Change the `input type="number"` to `text`, right now the chrome validation is kicking in before ng can and you can't see it in action – iamserious Sep 24 '15 at 08:34
  • 1
    @iamserious you could also cancel Chrome validation by using HTML5's novalidate attribute: `
    `, then you can/must do all validation checks with Angular.
    – Bart Jan 25 '16 at 11:14
  • The jsFiddle allows the user to enter and submit "+13", so it doesn't work. Why is that? Also I can enter "+14e1" - shouldn't pattern disallow that? I know javascript thinks that's a number but I shouldn't be able to input it. – Joel Peltonen Apr 07 '17 at 11:56
12

This is working

<form name="myform" ng-submit="create()">
    <input type="number"
           name="price_field"
           ng-model="price"
           require
           ng-pattern="/^\d{0,9}(\.\d{1,9})?$/">
    <span  ng-show="myform.price_field.$error.pattern">Not valid number!</span>
    <input type="submit" class="btn">
 </form>
Nishchit
  • 18,284
  • 12
  • 54
  • 81
0
<label>Mobile Number(*)</label>
<input id="txtMobile" ng-maxlength="10" maxlength="10" Validate-phone  required  name='strMobileNo' ng-model="formModel.strMobileNo" type="text"  placeholder="Enter Mobile Number">
<span style="color:red" ng-show="regForm.strMobileNo.$dirty && regForm.strMobileNo.$invalid"><span ng-show="regForm.strMobileNo.$error.required">Phone is required.</span>

the following code will help for phone number validation and the respected directive is

app.directive('validatePhone', function() {
var PHONE_REGEXP = /^[789]\d{9}$/;
  return {
    link: function(scope, elm) {
      elm.on("keyup",function(){
            var isMatchRegex = PHONE_REGEXP.test(elm.val());
            if( isMatchRegex&& elm.hasClass('warning') || elm.val() == ''){
              elm.removeClass('warning');
            }else if(isMatchRegex == false && !elm.hasClass('warning')){
              elm.addClass('warning');
            }
      });
    }
  }
});
Rajesh
  • 642
  • 2
  • 7
  • 14