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

  • Home
  • SEARCH
  • 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 3395536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:18:06+00:00 2026-05-18T04:18:06+00:00

Let’s assume we have a text-box on the page: <input type=text> The visitor types

  • 0

Let’s assume we have a text-box on the page:

<input type="text">  

The visitor types in some characters, and we read those characters via the value property of the text-box.

Now, let’s say that the text-box represents a numeric value. I cannot define such a thing on the text-box – its value is always going to be a string value. What I can do is, convert that sting value into a numeric value:

Number(input.value)

However, the input value is treated as a StringNumericLiteral, and not a NumericLiteral. That means that these input values are allowed (although they are not numeric literals in JavaScript):

  • values with leading zeros (like “000003”)
  • the “Infinity” value
  • values with a plus sign (like “+3”)

Now, let us say that – for the sake of the argument – I really want to read the input value as NumericLiteral. I don’t want numbers with leading zeros or the + sign, etc. I was thinking how I could accomplish this, and this is what I came up with:

try  {                  
    var n = JSON.parse('{ "n": ' + this.value + ' }').n;
    if (typeof n !== "number") {
        throw "error";
    }
    // n is a Number value that represents the value of the text-box        
} catch(e) {}

This is a simplified version of the code, a live demo is available here:
http://vidasp.net/tinydemos/input-as-number.html

Note: I use try-catch because if the input value is not a valid JSON value, the JSON.parse call will fail (throw an error).

So, what do you think of this method. Does it have flaws?

(One downside is that hex numeric values cannot be read, because JSON does not allow them.)

I just realized …

… that the reason why I was trying to do this was not to actually read the input as a number. What I wanted is to check whether the value complied to the NumericLiteral grammar – which is something that a regexp check can do easily.

  • 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-05-18T04:18:06+00:00Added an answer on May 18, 2026 at 4:18 am

    With JSON.parse you will be processing your value as a JSONNumber token, not really as a NumericLiteral.

    There are differences between JSONNumber and NumericLiteral, JSONNumber doesn’t allow leading zeros (no octals), nor hexadecimal literals, nor a leading dot (3.), or the opposite, a dot followed by the fraction digits (.3), moreover JSON.parse is really buggy across implementations, regarding numeric values, in SpiderMonkey (and in the json2.js library) JSON.parse('01') will not throw as one can expect.

    You could do validation to ensure the string complies with the NumericLiteral grammar, and if it does, you can use either eval or the Function constructor to really convert your value, for example:

    var numericLiteral = (function () {
      var numericLiteralSyntax = new RegExp([
        '^0x[\\da-fA-F]+$', // HexIntegerLiteral
        '^0[0-7]+$',        // OctalIntegerLiteral
        '^(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?$'//DecimalLiteral
      ].join('|'));
    
      return function (value) {
        if (typeof value != 'string') { throw TypeError('value must be a String'); }
    
        if (numericLiteralSyntax.test(value)) {
          return Function('return ' + value)(); // a NumericLiteral, evaluate it
        }
        throw SyntaxError('Invalid NumericLiteral'); // Not a NumericLiteral
      }
    })();
    
    numericLiteral('0xFF'); // 255, an HexIntegerLiteral
    numericLiteral('2e1'); // 20, DecimalLiteral with ExponentPart
    numericLiteral('3.'); // 3
    numericLiteral('.3'); // 0.3
    numericLiteral('3.1416'); // 3.1416
    numericLiteral('00010'); // 8, an OctalIntegerLiteral
    

    RegExps for validation are from SourceText a JavaScript Utility by Asen Bozhilov.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i

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.