-1

I'm trying to verify if a string is text or number. Could not find a proper way to verify. Could you please advice?

Here is my problem:

var myNumber = "006";
var myText = "1. This is not a number";

isNaN(myNumber); // false
isNaN(myText); // false

I tried also:
isNaN(myNumber.split('.')[1]);  // true
isNaN(myText.split('.')[1]); // true

parseInt(myNumber); // 6
parseInt(myText); // 1

What I would like to achieve would be to find when a string can be converted to a number (see myNumber). In case that the string is actually a text, how to spot it with javascript?

Could you please advise?

bjb568
  • 11,089
  • 11
  • 50
  • 71
addme
  • 31
  • 4
  • Try to see here http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript – feeeper Jul 08 '16 at 10:28
  • 2
    In my opinion the easiest way is to multiply the variable by 1 and check if the result is NaN, i.e. `isNaN( myVariable * 1 )` – pawel Jul 08 '16 at 10:36
  • `isNaN(myNumber)` returns true, not false. – JJJ Jul 08 '16 at 10:43
  • 2
    Possible duplicate of [Is there a (built-in) way in JavaScript to check if a string is a valid number?](http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) – JJJ Jul 08 '16 at 10:44
  • 1
    Please don't edit post titles to add solved. If an answer here has solved your issue, I suggest you mark an answer as resolved, otherwise leave a comment (Or even better, answer your own question) to direct others on how you solved your solution (As to help readers in the future). See this post for more info: http://meta.stackexchange.com/a/116105/295637 – Blue Jul 09 '16 at 15:00

7 Answers7

4

If I understood your question correctly I may have a solution but this will work even if the string is a number so here you go:

var yourNumber="55";
if(yourNumber*1==yourNumber){
 alert("It's a number");
}else{alert("it's not a number");}
Shrikantha Budya
  • 646
  • 1
  • 4
  • 15
  • This one does not work for me, as I'm using ExtendScript. What about the string yourNumber="55. bla bla bla" ? – addme Jul 08 '16 at 14:30
1

If parseInt() is not working for your desired results, you can try Number constructor. It gives you NaN for non-numbers, which you can verify by using isNaN() function.

var myNumber = "006";
var myText = "1. This is not a number";

console.log( Number(myNumber) );
console.log( Number(myText) );

Or you can use regular expressions:

 var myNumber = "006";
 var myText = "1. This is not a number";

 var numRegex = /^\d+$/;

 console.log( numRegex.test(myNumber) );
 console.log( numRegex.test(myText) );
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
1

You can use regex.

function isNumber(num){
  return /^(\d+)?(\.)?\d+$/.test(num);
}

isNumber("006") // true
isNumber(".6") // true
isNumber("1 not a number") // false
isNumber("23.63") // true
isNumber("23.6.3") // false
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
0
var reg = new RegExp('^[0-9]*$');
var myNumber = "006";
var myText = "1. This is not a number";
reg.test(myNumber) //true
reg.test(myText) //false
pawel
  • 35,827
  • 7
  • 56
  • 53
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • Hi pawel, thanks for this. I was also trying this way, but I forgot to include the $ sign. – addme Jul 08 '16 at 10:46
0

You can check if text is a number using isNaN function:

var myNumber = "006";
var myText = "1. This is not a number";

console.log(myNumber + ': ' + !isNaN(myNumber));
console.log(myText + ': ' + !isNaN(myText));
jcubic
  • 61,973
  • 54
  • 229
  • 402
0

The short story:

I would really use the typeof operator.

From isNaN() | JavaScript MDN:

The isNaN() function determines whether a value is Not-A-Number or not. ... you may alternatively want to use Number.isNaN(), as defined in ECMAScript 6, or you can use typeof to determine if the value is Not-A-Number.

Long story:

Tried the following in a node console.

A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.

isNaN(123)           // false
isNaN(true)          // false
isNaN("123")         // false
isNaN({})            // true
isNaN(undefined)     // true

In comparison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.

Number.isNaN(undefined)      // false
Number.isNaN({})             // false
Number.isNaN(true)           // false
Number.isNaN(123)            // false
Number.isNaN(NaN)            // true
Number.isNaN(0/0)            // true

The typeof operator returns a string indicating the type of the unevaluated operand.

typeof(123)           // number
typeof("123")         // string
typeof(true)          // boolean
0

Generally, the isNaN is the right idea, but you should parse it first:

var myNumber = "006";
var myText = "1. This is not a number";    

Number.isNaN(Number.parseInt(myNumber)); // false
Number.isNaN(Number.parseInt(myText));   // true
codecampo
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 10 '22 at 01:39