0

Possible Duplicate:
Is there a (built-in) way in JavaScript to check if a string is a valid number?

I have an input search bar in HTML

<form class="form-inline" action="javascript:displayResult()">          
    <input id="searchKey" >
    <button onclick="result()" type="button" class="btn btn-warning btn ">
          Go
    </button>                   
</form>

This is the Javascript function

function result() {     
    search($('#searchKey').val());    

    if(typeof(searchKey)=="number") { // checking if this is a number
       someFunction();
    }
};

The problem is , for each entry to the search bar I get the value as a string , even if it is "hello" or 9789 in the search bar . I used alert(typeof(searchKey)); to verify and it always return the type as string

I am trying to differentiate between a number and a string at the search bar , I am sure there is a better way to do this , but I am unsure about why this is not working

I cannot use parseInt() as I need to differentiate between text and number dynamically

5
  • 1
    When would code after a return statement get executed? Commented Aug 4, 2012 at 8:26
  • You are calling it after return man! Commented Aug 4, 2012 at 8:27
  • apologies , I ave edited my code Commented Aug 4, 2012 at 8:27
  • is searchKey a global variable? Commented Aug 4, 2012 at 8:27
  • try parseInt(str, base) if it fails it should just return 0 Commented Aug 4, 2012 at 8:37

3 Answers 3

5

The value of a <input type="text"> is always a string. You can, however, parse it as an number by using parseInt. It will result in NaN if not number could be parsed from the string. Don't forget you'll have to use isNaN for this, as myNumber === NaN isn't a valid operation.

Even better, use isNaN(+searchKey), as the unary operand will call ToNumber. However, this will ignore whitespace.

9.3.1 ToNumber Applied to the String Type

ToNumber applied to Strings applies the following grammar to the input String. If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

[see http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf for the actual rules]


11.4.6 Unary + Operator

The unary + operator converts its operand to Number type. The production UnaryExpression : + UnaryExpression is evaluated as follows:

  1. Let expr be the result of evaluating UnaryExpression.
  2. Return ToNumber(GetValue(expr)).
Sign up to request clarification or add additional context in comments.

3 Comments

it' sometimes cannot work . like "123dwabc".
@blueiur: You could either use parseInt(val).toString() == val to make sure that both values are the same or use a regular expression.
i think use parseFloat insteadof parseInt :)
3

use

if(!isNaN(searchKey))

instead of

if(typeof(searchKey)=="number")

Comments

3

A text input field always contains a string value. Try if (!isNaN(searchKey)) to test for numeric value. Alternatively you could use a Regular Expression: /^\d+$/.test(searchKey)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.