0

UPDATE: [SOLVED] Thanks everyone for answering me, I have tried two method and it is really work! Here are jsbin for them:

This one was inspired by Secret's answer

This one is from Alexander's O Mara's answer

I'm trying to check if the entered value is number or not. I'm using typeof to check it. If entered value is number, alert('number');. But if entered value is string, alert(’string');.

The problem is, it always alert(’string'); although the entered value is number.

What's wrong? How can I solve it?

Here is my code:

<input type="text" placeholder="inputText" id="inputText">
<input type="button" onclick="alertType()" value="alertType">
<script>
 function alertType()
 {
  var someStr = document.getElementById("inputText").value;
  var someStrType = typeof someStr;
  alert(someStrType);
 }
</script>
Anakin
  • 1,233
  • 1
  • 14
  • 20
  • See [Validate decimal numbers in JavaScript - IsNumeric()](https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) – Chris Feb 28 '15 at 05:52
  • Define "number" Do you mean an integer? A decimal? Can it be negative? – aquinas Feb 28 '15 at 05:55
  • possible duplicate of [Get the type of a input from an HTML form](http://stackoverflow.com/questions/19513041/get-the-type-of-a-input-from-an-html-form) – Gildas.Tambo Feb 28 '15 at 05:59

5 Answers5

2

You could simply cast the string to a number using Number, and use isNaN to check if it was successfully cast to a number. Number will return NaN if it fails to cast to a number, which isNaN can detect.

<input type="text" placeholder="inputText" id="inputText">
<input type="button" onclick="alertType()" value="alertType">
<script>
 function alertType()
 {
  var someStr = document.getElementById("inputText").value;
  var someNum = Number(someStr);
  if (isNaN(someNum)) {
    alert('not a number');
  }
  else {
    alert('a number');
  }
 }
</script>
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
1

Problem here is your data source is textbox which gives value as string. You need to parse it to number.

1
var my_numberA = 3;     //  This is typeof number
var my_numberB = '3';   //  This is typeof string

So depending on how you write it: a 3 is a number where '3' is a string

The trick is to do the following:

Any string multiplied by 1 will convert the string to a number. However, multiplying a string that can not be represented by a number will return an error. So you need to compensate for that possibility with a try{}catch(err){} that converts the string to a number if possible

function convertToNumber(input){
    try{var output = 1*input}catch(error){var output = input}
    return output
}

var my_numberA = 3;     //  This is typeof number
var my_numberB = '3';   //  This is typeof string

console.log( typeof my_numberA ) // Will show 'number'
console.log( typeof my_numberB ) // Will show 'string'

console.log( typeof convertToNumber(my_numberA) ) // Will show 'number'
console.log( typeof convertToNumber(my_numberB) ) // Will show 'number'

I prefer this in some cases rather than the Number() function because it returns the same passed object instead of returning NaN.

FactoryAidan
  • 2,484
  • 1
  • 13
  • 13
0

The thing is, when you get the inputText value from your dom, it is a string. For example, "12" is a string, vs. a 12

What you can do is to check if it is not a number:

function alertType()
 {
  var someStr = document.getElementById("inputText").value;
  if(isNaN(someStr)){
     //it's not a number!
  } else  { 
     //it's a number!
  }
 }
Secret
  • 3,291
  • 3
  • 32
  • 50
  • Be careful though, isNaN returns `false` for things like `0xdeadbeef`, an empty string, etc. Depending on what you want, a regex is often a better choice... – aquinas Feb 28 '15 at 05:53
0

Try this ...

$('#clickMe').click(function () {
    var inputVal = $("#inputText").val();
    var value = Number(inputVal);

    if (isNaN(value)) {
        alert('not a number');
    } else {
        alert('a number');
    }
});

https://jsfiddle.net/mky9vxdy/1/

R4nc1d
  • 2,923
  • 3
  • 24
  • 44