2

I have an input field and one button. When the user clicks the button, first I check whether they entered a number or not. If it is a non-numeric string then nothing should happen. If it's a number then I will do some modification to the above gray div. When I enter a number and log its type to the console, it shows that the data type of the value is "string." So how do I check for the correct data type?

Here is the code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id='test'></div>
  <p><input type='text' id='myText'/>
  <input type='button' value='click' id='myButton'/></p>
</body>
</html>

CSS

#test {
  width: 200px;
  height: 200px;
  background: gray;
}

JavaScript

var el = document.getElementById('myButton'),
    elOne = document.getElementById('myText'),
    container = document.getElementById('test');

el.onclick = function(){
  var data = elOne.value;

  if(typeof data === 'number'){
    console.log('number');
  }else if(typeof data === 'string') {



     console.log('string');
  }
};

And here is the jsbin link - http://jsbin.com/hequr/1/

Lochlan
  • 2,666
  • 3
  • 23
  • 25
naresh kumar
  • 2,165
  • 3
  • 19
  • 21

1 Answers1

0

use functions like isNaN() or isNumeric() or use Regular expression \[0-9].*\