2

I tried to search here but seems I can't find the best answer to my problem.

How can I validate if the user input is double, Float or Long (data types in JAVA) in javascript?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Noj
  • 23
  • 5

6 Answers6

3

If you want to check for an object type you can use typeof keyword of javascript. For example if you want to check for a number you can do something like this:

typeof i === 'number'

or using regex for floating types:

^\d{0,2}(\.\d{0,2}){0,1}$
V31
  • 7,626
  • 3
  • 26
  • 44
1

I don't think there is such a difference in javascript.

Does JavaScript have double floating point number precision?

Community
  • 1
  • 1
inf3rno
  • 24,976
  • 11
  • 115
  • 197
1

There are not such types in javascript.

The types you said in javascript is a primitive data type called Number.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • those datatypes are in java language. i just want to validate the data in javascript. – Noj Feb 28 '14 at 05:39
1

In JavaScript, all numbers are 64-bit floats. Functions like parseInt() treat their input like a signed int, but create a float. And bit-wise operators recreate the same behaviour you would expect with ints, but on floats.

jordancpaul
  • 2,954
  • 1
  • 18
  • 27
0

The Javascript number primitive can't represent the range of values that the combination of Javas int, long and double can. If you really need to validate this anyway you could always write some kind of hack. For example, for the Java long you could:

store javaLongMaxValue as a string, interpret your input number as a string, left pad your input string with "0" until it is javaLongMaxValue.length long and then compare the strings.

You would of course have to validate that the input can be interpreted as a number and handle the case when it's negative.

Josef Engelfrost
  • 2,955
  • 1
  • 29
  • 38
-1

use if statements and parse it

parseInt('var'); 
parseDouble('var');

so on..

example..

if(parseInt('var')){ 
      // code above checks if it is a integer.. returns 'true' if yes and 'false' if not 
      // then your code
}else if(parseDouble('var')){
      // your code
}

.. so on