Create a regex that includes decimal numbers and a maximum of length - javascript

I'm trying to create a regex pattern that accepts decimal numbers and the maximum length should be 3.
These are the regex I tried but didn't work
new RegExp('d{1-3}')
new RegExp('^[0-9]{3}$')
I want to achive to allow the decimal numbers between 0-999.
For example 185.5
Thanks in advance.

you can try this: ^[1-9]{0,3}$
it only accept numbers with max length of 3
example: 123
and if you want it with complex numbers use this:
^[1-9]{0,3}([,.][0-9]{0,3})?$
example: 154.234

here is one way to do it, which matches any number b/w 0 and 999 including decimal values (unlimited)
^(\d{,3}(\.)?(?(2)\d*|$))$
# should start and ends with a digit
# \d{,3} : match at most 3 digit
# (\.)? : optional period
# ?(?(2) : if period exist match the following pattern
# \d* : match any number of digits after period
# $ : else it should be end of pattern
https://regex101.com/r/B8jRG6/1

regExp ^\d+((\.)|(\.\d{1,3})?)$

Related

Regular expression to validate string format <whole-number>#<whole-number>

I need to create a regular expression for a string in the format
<whole-number>#<whole-numbers>%
for example:
1#100%,
9#50%,
5#10%,
only single numeric digit should be allowed before #
only # special character after the numeric digit
there should be only 3 or less numeric digits after the #
only % special character should be allowed at the end of the string.
the following examples are not valid.
0#100%,
a#50%,
1#abc%,
I have created a regular expression but it is not working as expected
([0-9]{1}[#]{1}[0-9]{0,3})
This should do it:
/^\d#\d+%$/
\d is for digits. + says one or more characters
The way you have written your regex, it seems you only need 3 numbers after #, if you want that then the way in which you have written it, it should be something like:
/^[0-9]{1}#[0-9]{0,3}%$/
You were missing the % character match and the anchor tags.
Or else you could just use:
/^\d#\d{0,3}%$/
You want the string to start with a one digit number from 1 to 9 (not 0), followed by #, then by a number with at most 3 digits and ending with %.
Then use:
/^[1-9]#\d{1,3}%$/
console.log('4#55%',/^[1-9]#\d{1,3}%$/.test('4#55%'))
console.log('2#678%',/^[1-9]#\d{1,3}%$/.test('2#678%'))
console.log('22#22%',/^[1-9]#\d{1,3}%$/.test('22#22%'))
console.log('a#11%',/^[1-9]#\d{1,3}%$/.test('a#11%'))
console.log('0#99%',/^[1-9]#\d{1,3}%$/.test('0#99%'))
console.log('3#%',/^[1-9]#\d{1,3}%$/.test('3#%'))
console.log('1#abc%',/^[1-9]#\d{1,3}%$/.test('1#abc%'))

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+$

JS Regex to allow only numbers, semicolon and hyphen

I am building an application which should accept strings only with the following formats:
12345 (only a number)
12345;23456 (two or more numbers separated by ;)
12345-12367 (a range of numbers separated by a -)
The java script regex should allow only the above formats & shouldn't accept any other formats or symbols . Can anyone come up with a regex for this?
This is the RegExp that you need: /^\d+((;\d+)*|-\d+)?$/
(;\d+)* will check for multiple numbers separated by ";"
-\d+ will check for a range
Try
^[0-9]+([;-][0-9]+)?$
That should work
[0-9]+ matches 1 or more digits
[;-] matches a ; or a -
(...)? is an optional match
^ anchors the start and $ anchors the end of the string
^[0-9-;]{0,50}$
0-9 only accept numbers
-; allow only - and ;
{0,50} allow only 50 chars
Assuming that the number portions you are looking for are 5 digits each time, the following should match what you want.
[0-9]{5}((;|-)[0-9]{5}){0,1}
If you need different lengths, you can update the {5} with either another fixed length or a range such as {3,5} for a string of 3 to 5 digits. If you want to be able to capture more than two numbers with the speperators listed, you can use
[0-9]{5}((;|-)[0-9]{5})*

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?$/

Javascript RegEx Help

Can someone help me to validate the following rules using a RegEx pattern
Max length : 15
Minimum length : 6
Minimum character count : 1
Minimum numbers count : 1
Consequent repeated character count : 2
^ # start of string
(?=.{6,15}$) # assert length
(?=.*[A-Za-z]) # assert letter
(?=.*[0-9]) # assert digit
(?:(.)(?!\1\1))* # assert no more than 2 consecutive characters
$ # end of string
will do this. But this won't look nice (or easily maintainable) in JavaScript:
if (/^(?=.{6,15}$)(?=.*[A-Za-z])(?=.*[0-9])(?:(.)(?!\1\1))*$/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
I suggest you use a few different regex patterns to check for all those rules cause it will either be impossible or very complicated.
.length to check the first 2 rules
[a-z] (with case insensitive option) for the 3rd rule
\d for the 4th rule
(.)\1{2,} for the 5th rule, if this one matches the string contains 3+ character repetitions

Categories

Resources