tk0221
tk0221

Reputation: 121

AngularJS input to accept integers and special characters

<input type="text" ng-pattern="/^[0-9]*$/" id="players" name="players" ng-model="team.players"/>

I'd like the above input to accept integer numbers along with the following special characters. . + -

How can I filter this with minimum code?

I tried some examples found on Internet, with none of them are working correctly whatsoever.

Note: My goal is to accept integer values (along with the special characters mentioned above) but not decimals.

Upvotes: 0

Views: 775

Answers (2)

tk0221
tk0221

Reputation: 121

I try regex, but it didn't work. I end up with this code below,

$scope.reg.players=parseInt($scope.reg.players)

So when user put 1.2 it will convert to 1. If user type 1. then it print invalid input error.

Upvotes: 0

sapanoia
sapanoia

Reputation: 789

Let us assume that want to accept the following numbers, which are all integral: 37, -10, -09, +23, 7., 7.000.

The following regex:

^[+\-]?[0-9]+(\.0*)?$

will accept an optional leading sign, one or many digits, and an optional decimal point with only trailing zeroes. It will deliberately not accept numbers without any digits before the decimal point, e.g. .00. The regex is anchored with ^ and $ to ensure that the whole string matches.

Depending on the regex dialect you may have to quote more or less characters.

Upvotes: 1

Related Questions