最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to check input value has only numbers in angular? - Stack Overflow

programmeradmin2浏览0评论

I want to pare a value of a field centerCode which is entered by a user into an input field. Then I want to check if it is a number and show the appropriate alert accordingly. I am not able to pare the value or variable number with the variable code .

 var numbers =/^[0-9]+$/;
 var code = $scope.Nuser.centerCode;
 alert(code);
 if(code.val(numbers))
 {
     alert(code.val(numbers));
 }
 else
 {
     alert("enter numbers only");
 }

I want to pare a value of a field centerCode which is entered by a user into an input field. Then I want to check if it is a number and show the appropriate alert accordingly. I am not able to pare the value or variable number with the variable code .

 var numbers =/^[0-9]+$/;
 var code = $scope.Nuser.centerCode;
 alert(code);
 if(code.val(numbers))
 {
     alert(code.val(numbers));
 }
 else
 {
     alert("enter numbers only");
 }
Share Improve this question edited Jul 15, 2015 at 2:44 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Jul 6, 2015 at 9:51 AnkyAnky 2702 gold badges7 silver badges23 bronze badges 4
  • Is it possible for you to use an input number instead of an input field ? Could be way easier – Pierre-Alexandre Moller Commented Jul 6, 2015 at 9:54
  • I agree wit @Apédémak, it would be much easier to use an input number. If you really wanted to, however, you could loop over the string checking if it contains numbers 0-9. – BobDoleForPresident Commented Jul 6, 2015 at 9:56
  • Instead of code.val(numbers) try new RegExp(numbers).test(code) – Mayank Saxena Commented Jul 6, 2015 at 9:59
  • Instead of checking you can restrict the user to give appropriate value. Check this low size plugin, which will help you solve the issue.. github./Jeevanandanj/angular-input-decimal-separator – Jeeva J Commented Feb 22, 2016 at 10:39
Add a ment  | 

4 Answers 4

Reset to default 3

You're along the right lines. Numbers needs to be a Regex though, and you need to use the test function to check the input against it. The test function will return true if the string is all numbers, or false if there is anything else in it.

var numbers = new RegExp(/^[0-9]+$/);
var code = $scope.Nuser.centerCode;

if(numbers.test(code))
{
    alert('code is numbers');
}
else
{
    alert("enter numbers only");
}

I would suggest you to use ng-pattern instead . Something like following :

<input type="text" ng-pattern="/^[0-9]{1,7}$/" ng-model="inputNumber"/>

It will only allow the user to enter the number.

You can use angular.isNumber to check if the entered value is a number or not. Try something like the following :

if(angular.isNumber($scope.Nuser.centerCode)){
   alert('Center Code is a number');
}else {
   alert('Center Code is not a number');
}

Hope this will do the trick.

You can simply convert string to number and test is it's NaN (Not a Number)

isNaN(+$scope.Nuser.centerCode)

if it's false it means your centerCode contain only numbers

try this hope this is what you are looking for

 if(angular.isNumber(value))
       { 
        //your code here 
       }
发布评论

评论列表(0)

  1. 暂无评论