validate natural input number with ngpattern

匿名 (未验证) 提交于 2019-12-03 02:43:01

问题:

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.

Not valid number!

回答1:

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:

Not a valid number! This field is required!

JS:

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

Here is a jsFiddle demo.



回答2:

This is working

Not valid number!


回答3:

 Phone is required. 

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');             }       });     }   } }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!