Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:12:26+00:00 2026-06-09T01:12:26+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:12:28+00:00Added an answer on June 9, 2026 at 1:12 am

    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)).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there a (built-in) way in JavaScript to check if a string
Possible Duplicate: Is There A Built-In Way to Split Strings In C++? i have
Possible Duplicate: Convert exponential number to decimal in php Is there a way to
Possible Duplicate: Is there a way to have content from an IFRAME overflow onto
Possible Duplicate: Is there any hash function in PL/SQL? I have a column with
Possible Duplicate: Is there a way of drawing a caption box in matplotlib Is
Possible Duplicate: Is there any working memory profiler for Python3 I have some script
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: Is there an easy way in .NET to get “st”, “nd”, “rd”
Possible Duplicate: C# string formatting and padding In my C# program, I have four

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.