admin管理员组文章数量:1354389
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
4 Answers
Reset to default 3You'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
}
本文标签: javascripthow to check input value has only numbers in angularStack Overflow
版权声明:本文标题:javascript - how to check input value has only numbers in angular? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743912932a2560722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论