Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in angularjs if ng-model has a value of string or a number?

I have my java script in angular checking if the value of text field is undefined or empty, it is fine and working,

$scope.checkNumber = function(user_answer){
	if(user_answer == undefined){
	  return false;					
	}
}

But my next step is how I can make a function to identify if the value has a string or a number and return a boolean. I don't know the right syntax for angular java script, can anyone help me to solve my problem?

like image 529
JustAce Avatar asked Mar 15 '23 15:03

JustAce


2 Answers

You can do this the angular way, using the angular helper functions:

$scope.checkNumber = function(user_answer){
    if(angular.isUndefined(user_answer)){
      return false;                 
    }
    if(angular.isString(user_answer)) {
       //return boolean
    }
    if(angular.isNumber(user_answer)) {
       //return boolean
    }
}
like image 97
Robin-Hoodie Avatar answered Apr 06 '23 00:04

Robin-Hoodie


Try like this

function check(text){     
 return typeof text ==="number";
}
like image 40
Anik Islam Abhi Avatar answered Apr 06 '23 01:04

Anik Islam Abhi