1

In Javascript, is there a way to check or validate the datatype of a variable? I need to allow users to enter float values in the textbox.

Thank you.

3
  • 2
    Did you do a simple Google search before posting here? This has been asked and answered countless time on the web. Commented Dec 8, 2012 at 4:12
  • Sorry for asking this question. Since I am a beginner in javascript I was not able to get a clear thing out there in google. Anyway thank you stackoverflow. Commented Dec 8, 2012 at 4:18
  • I was going to +1 because it's a valid question, and then -1 for being lazy because the answer can be easily found in numerous resources. This is basic form validation that should come with your development environment of choice. Commented Dec 8, 2012 at 4:19

2 Answers 2

8

If you're dealing with literal notation only, and not constructors, you can use typeof:.

Example:

>var a = 1;
>var b = "asdasd";
>typeof(b);
"string"
>typeof(a);
"number"

---EDIT---

To validate numbers or float values use:

function isNumber (n) {
  return ! isNaN (n-0);
}

Example:

>var a = 1;
>isNumber(1);
True

Float Included, use parsefloat:

function isIntandFloat(n) {
   return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
}

Or if you want just float use this:

function Float (n) {
  return n===+n && n!==(n|0);
}

Example:

>var a = 0.34324324324;
>Float(a);
true
>var int = 3;
>Float(int);
false
Sign up to request clarification or add additional context in comments.

3 Comments

He doesn't actually mean the type. He wants users to enter just numbers, optionally with a decimal value.
for float value validation is there any similar function?
@user1671639 Yes, there is, I added it in my answer.
1

A text box will always give you a string primitive value.

What you want is to see if the input can be converted from a string to a number. For this you can use parseFloat().

var num = parseFloat(textbox.value);

if (isNaN(num)) {
    alert("Invalid input");
}

If you want more strict evaluation, use the Number function

var num = Number(textbox.value);

if (isNaN(num)) {
    alert("Invalid input");
}

6 Comments

Why not bind to key down and block letters?
But parseFloat() doesn't tell you if the entered value actually is a number, because parseFloat("123abc") returns 123. @Jeroen - that doesn't prevent pasting and drag'n'drop of invalid values.
@nnnnnn: That's true, but it is convertible, providing a valid number. It depends on what OP wants to accept as input.
Why don't you use +textbox.value? It seems to fail on an input of "123abc" yet not need to create a Number object
@Ian: Yes, the unary + could be used too, but Number is a little clearer for a beginner. Using Number doesn't actually create an object. We still get the number primitive. It's only if new is used that we get the object wrapper. :-) es5.github.com/#x15.7.1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.