userMod2 userMod2 - 2 years ago 106
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download