userMod2 userMod2 - 1 year ago 63
AngularJS Question

Angular - Check if value entered into a field is a number

In my Angular app I have a field that accepts either an email address or a phone number.

If a number is entered I want to show a particular span. However I can't seem to get this working.

So in my controller I have written:

$scope.isNumber = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);

Then in my HTML I have:

<form name="registrationForm">

Email or Mobile
<span ng-show="isNumber(registrationForm.usernameReg)">Include the country code.</span>

<input name="usernameReg" ng-change="main.change()" type="text" ng-model="main.username">


Any ideas why this wouldn't work?


Answer Source

You need to be testing the value of the model.



registrationForm.usernameReg is an object that contains numerous properties used as part of validation.

To better see this add <pre>{{registrationForm |json}}</pre> in your view