Only one minus symbol for negative numbers JS regex - javascript

I have this code /^[-0-9\b]+(\.\d{0,2})?$/ for the positive and negative numbers validation
It's working fine for the negative numbers(for ex -12.34), but user can also type a few "-" symbols(ex: -12-3-4.12).
So how can I disable a possibility to type a multiple minus symbols?

You seem to be using the regex for liva validation. In these cases, the regex should comprise only optional parts, those that can match an empty string.
You can use
/^-?\d*\.?\d{0,2}$/
See the regex demo. Details:
^ - start of a string
-? - an optional -
\d* - zero or more digits
\.? - an optional .
\d{0,2} - 0, 1 or 2 digits
$ - end of string.

[-0-9\b]+ Is making it possible to have one or more - characters.
Just change your regex to:
/^-?[0-9\b]+(\.\d{0,2})?$/
Here is the test:
https://regex101.com/r/fbMI7Z/1/

Related

Regex for input with numbers and commas

I'm trying to limit input data.
My goal:
only two symbols per input allowed: numbers and a comma
first symbol only number (zero or more)
amount of numbers is unlimited (zero or more)
a dangling comma is allowed but only one
Test cases:
1,2,4 - ок
1221,212,4121212 - ок
,2,3 - not ок
1,2,3, - ок
11,21111,31111, - ок
I've hade something like this but it doesn't work properly
/^\d*(,\d*)*$/.test(value)
Appreciate any help!
You can use
/^(?:\d+(?:,\d+)*,?)?$/
See the regex demo. Details:
^ - start of string
(?:\d+(?:,\d+)*,?)? - an optional non-capturing group:
\d+ - one or more digits
(?:,\d+)* - zero or more sequences of a comma and one or more digits
,? - an optional comma
$ - end of string.

Generic JavaScript regex validating a positive number with or without commas as thousand separators and an optional fractional part

I have following regex to validate numbers in input
var reg = /^\d+$/;
Now i want to allow ,(commas) and .(period) in number field as following will some one help me writing regex to allow following number format ?
10000000
10,000,000
10000000.00
You may use
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
See the regex demo
If you only need to allow 2 digits after the decimal separator, replace (?:\.\d+)? with (?:\.\d{1,2})?.
Details:
^ - start of string
(?:\d{1,3}(?:,\d{3})*|\d+) - 2 alternatives:
\d{1,3}(?:,\d{3})+ - 1 to 3 digits and one or more sequences of a comma and 3 digits
\d+ - 1 or more digits
(?:\.\d+)? - an optional sequence of:
\. - a dot
\d+ - 1 or more digits
$
You could use
^((\d{1,2}(,\d{3})+)|(\d+)(\.\d{2})?)$
see Regex101
or
^((\d{1,2}(,\d{3})+)|(\d+))(\.\d{2})?$
if you want 10,000,000.00 to get matched to.

Regular expression for thousand separator without decimal point

I have regular expression which is working fine for decimal .
In below expression I want to remove decimal match to validate Integer
^-?(\d*)(,\d{1,3})*(?:[,]|([.]\d{0,2}))?$
Valid Match : 1,000 & 111, & -1000,00
Invalid Match : -,1 & 1,,,,
Use
^-?(?:\d{1,3}(?:,\d{3})*|\d+)?,?$
|---1---|
|---2-----|
|-3-|
The ^ asserts the position at the start of the string and $ asserts the position at the string end. -? matches an optional -. Part 1 matches 1 to 3 digits, Part 2 matches 0+ sequences of , followed with 3 digits, part 3 is an alternative to Part 1 & 2 combined and matches just 1+ digits. The whole number pattern is optional since the (?:...|...) is quantified with ? - one or zero occurrences.
The ,? before $ allows an optional comma at the end, delete if not required (you mentioned you need to allow 100, in the comments).
See the regex demo

how to accept negative values for amount textfield with this regular expression

I want to accept a negative value to the text field by without disturbing the functionality for following regular expression :
(?!^0*$)(?!^0*[.]0*$)^[0-9]{1,8}([.][0-9]{1,2})?$
for ex : -12.12, -1223233.23, -32323.32
Thanks.
Your regex has lookaheads that are each triggering at every location inside a string. To make the regex more efficient and easily adjustable for a fix like the one you need, you need to move the ^ out of the lookaheads: ^(?!0*$)(?!0*[.]0*$)^[0-9]{1,8}([.][0-9]{1,2})?$.
Now, you need to add an optional minus at the start. "Optional" means 1 or 0 occurrences. In JavaScript, you can use a ? quantifier for that (in POSIX BRE, you would have no other alternative but \{0,1\}).
So, use
^-?(?!0*$)(?!0*[.]0*$)[0-9]{1,8}([.][0-9]{1,2})?$
See the regex demo
The regex breakdown:
^ - start of string
-? - 1 or 0 hyphens
(?!0*$) - make sure there are no zeros up to the end of string (return no match if a zero is found)
(?!0*[.]0*$) - make sure there are no zeros + a dot + zeros up to the end of string
[0-9]{1,8} - match 1 to 8 digits
([.][0-9]{1,2})? - 1 or 0 sequences of...
[.] - a literal dot
[0-9]{1,2} - 1 to 2 digits
$ - end of string.
Just add -? to make the negative sign optional, or simply - if it's mandatory.
(?!^-?0*$)(?!^-?0*[.]0*$)^-?[0-9]{1,8}([.][0-9]{1,2})?$
Edit: Fixed
THis validates an integer (positive or negative)
^-{0,1}\d+$
This validate a decimal number (positive or negative)
^-{0,1}\d*\.{0,1}\d+$

Regular Expression not working as expected in javascript

I have following regular expression: ^-?([0-9]{0,3}$|[0-9]{0,2}\.?[0-9]{0,1}$)
It should not allow 4 digit number such as 4444. The expression is working fine if I try here, but in javascript, the code is not working as expected. It is allowing 4 digit numbers. All other validations work fine.
Here is my code:
http://jsfiddle.net/PAscG/
reg0str = "^-?([0-9]{0,3}$|[0-9]{0,2}\.?[0-9]{0,1}$)";
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return true;
UPDATE TO EXPLAIN Functionality:
I want to allow only 3 digits. So either I can allow only 1 digit after decimal and 2 before decimal or I can allow max of 3 digits before decimal and nothing after decimal.
So my first part:
[0-9]{0,3}$ I assume this should allow a max of 3 digits and only numbers.
Next part: [0-9]{0,2}\.?[0-9]{0,1}$ should allow max of 2 digits before decimal and a max of 1 digit after decimal.
Following OP's clarification
The regexp is
/^-?(\d{0,3}\.?|\d{0,2)\.\d)$/
^ start of string
-? optional minus sign (use [-+]? if you accept a plus sign)
( start of OR group
\d{0,3} 0 1, 2 or 3 digits
\.? optional decimal point
| OR
\d{0,2} 0 1, or 2 digits
\. decimal point
\d final decimal
) end of OR grouping
$ end of string
Try this:
var reg0str = "^\-?[0-9]{0,2}[\.]?[0-9]?$";
I'm not sure why, but the period seems to be being treated as the wildcard character if not encapsulated within a class.
Here's the updated jsfiddle
"…\.…" is a string literal - the backslash escapes the dot to a dot and the regex dot matches a digit. You would need to escape the backslash to pass a string with a backslash in the RegExp constructor:
new RegExp("^-?([0-9]{0,3}$|[0-9]{0,2}\\.?[0-9]{0,1}$)")
or you use a regex literal (simplified, but still matching the same):
/^-?\d{0,2}\.?\d?$/

Categories

Resources