Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if value is number

Tags:

angularjs

I have this function :

 $scope.SearchTicketEvent = function (ticketPinOrEvent)
            {
                if (ticketPinOrEvent != undefined)
                {

                 if (ticketPinOrEvent.length == 10)
                    {

                     $scope.PinTicketSearch(ticketPinOrEvent);

                    }
                }


            }

How can i check if ticketPinOrEvent is number ? I tried with angular.isNumber(ticketPinOrEvent) but i dont get anything?

like image 397
uzhas Avatar asked Jul 22 '15 08:07

uzhas


3 Answers

If you want to use angular.isNumber

    if ( !isNaN(ticketPinOrEvent) && angular.isNumber(+ticketPinOrEvent)) {
    }
like image 82
dhavalcengg Avatar answered Oct 07 '22 12:10

dhavalcengg


You might use the typeof to test if a variable is number.

if (typeof ticketPinOrEvent === 'number') {
    $scope.PinTicketSearch(ticketPinOrEvent);
}

Or might try this:

if (!isNaN(ticketPinOrEvent) && angular.isNumber(ticketPinOrEvent)) {
    $scope.PinTicketSearch(ticketPinOrEvent);
}

Testing against NaN:

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

like image 20
Endre Simo Avatar answered Oct 07 '22 14:10

Endre Simo


In Angular 6 this works without using any prefix.

Example:

if(isNumber(this.YourVariable)){
    // your Code if number
}
else {
    // Your code if not number
}
like image 31
MarmiK Avatar answered Oct 07 '22 13:10

MarmiK