最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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;					
	}
}

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?

Share Improve this question edited Feb 13, 2019 at 3:02 JustAce asked Jun 25, 2015 at 5:07 JustAceJustAce 2441 gold badge6 silver badges18 bronze badges 3
  • have you tried typeof , its inbuilt into JS – Sushant Commented Jun 25, 2015 at 5:21
  • if you want to have a number only textfield, use <input type='number'> – Icycool Commented Jun 25, 2015 at 5:38
  • @lcycool <input type='number'> doesn't prevent you from inputting strings – Robin-Hoodie Commented Jun 25, 2015 at 5:59
Add a ment  | 

2 Answers 2

Reset to default 4

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
    }
}

Try like this

function check(text){     
 return typeof text ==="number";
}
发布评论

评论列表(0)

  1. 暂无评论