Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer assumes you wish to validate a decimal "number" which meets the following criteria:</p> <h2>Definition of a "Number"</h2> <ul> <li>There must be at least one digit and all digits must be decimal (from 0 to 9).</li> <li>Both the integer and fractional parts are optional, but one or the other must be present.</li> <li>The fractional part, if present, may have any number of digits but is always preceded by a decimal point.</li> <li>If the integer part has more than one digit, the leading digit must not be zero.</li> <li>If the integer part has more than three digits, commas may be used to separate the integer into digit triplets.</li> <li>The number may begin with an optional sign, either a plus <code>+</code>, or a minus <code>-</code>.</li> <li>Whitespace is not allowed.</li> <li>An exponent part is not allowed.</li> </ul> <p>Given the above requirements here are some valid and invalid numbers</p> <h2>Valid "Numbers":</h2> <pre><code>0 0. .0 0.0 1 +1 -1 1234 123456 1,234 12,345 123,456 123,456.7890 </code></pre> <h2>Invalid "Numbers":</h2> <pre><code>0FA92 // Invalid digit. Must be 0-9. 0123 // Multi-digit integer must lead with non-zero. . // Must have at least one digit. 1,23,4 // Commas must separate triplets of digits. 12345,678. // Missing comma. + 10 // Whitespace not allowed. 1.2E34 // Exponents not allowed. </code></pre> <p>Here is a regex (first presented in commented, free-spacing mode format using Python's handy raw string syntax) which matches a number meeting the above requirements:</p> <h2>Regex to validate a "number"</h2> <pre class="lang-py prettyprint-override"><code>r""" ^ # Anchor to start of string. [+\-]? # Optional sign. (?=\.?\d) # Must have at least one digit. (?: # Optional integer part. Either... [1-9] # A sequence of digits w/no commas. \d* # (but first digit is not zero.) | # or an integer having commas... [1-9] # First digit is not zero. \d{0,2} # 1,2 or 3 digits ahead of 1st comma. (?: # One or more comma + 3 digits. , # Comma separates \d{3} # digit triplets. )+ # One or more comma + 3 digits. | 0 # or integer part may be just zero. )? # Optional integer part. (?: # Optional fractional part. \. # Dot separates \d* # zero or more fraction digits. )? # Optional fractional part. $ # Anchor to end of string. """ </code></pre> <p>Here is a JavaScript function which implements the above regex:</p> <h2>function isValidNumber()</h2> <pre class="lang-js prettyprint-override"><code>function isValidNumber(text) { var re = /^[+\-]?(?=\.?\d)(?:[1-9]\d*|[1-9]\d{0,2}(?:,\d{3})+|0)?(?:\.\d*)?$/; if((re.test(text))) return 'true'; return 'false'; } </code></pre> <p>Note that you should not be trying to validate the number until after the user has finished entering the entire number. Calling a validation function for each and every keypress (as your code above appears to be doing) is not good practice IMHO.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload