Cannot fathom out why Javascript regex not working - javascript

I want to validate some input to check that it is a positive currency value of the format:
0.5, 0.55, 5 (note this one), 5.5, 5.55, 55 etc etc.
The code that I'm using is:
if ($("#gross").val()>0 && !/^\d+?\.?\d?\d$/.test($("#gross").val())) {
alert($("#gross").val() + " is invalid currency");
}
It works for everything except a single digit, eg 5 (and 5.) but does work for 5.5.
What am I doing wrong?

You've forgotten to add a ? at the end, before the $. A better way of doing it would be the following:
/^\d+?\.?\d{0,2}$/
This checks that there are up to two decimal places for the number - if you'd like to check for any amount, you could use something like:
/^(?!\.$)(?:(?!0\d)(\d*)\.?(\d[0-9]*))$/
Note that it's a good idea to explicitly convert your string into a number, and also cache the value of #gross.
var grossVal = $("#gross").val();
if (+grossVal > 0 && !/^\d+?\.?\d{0,2}$/.test(grossVal)) {
alert(grossVal + " is invalid currency");
}

+? will match the fewest possible matches, in this case, 1 digit.
I think you're looking for something like:
/^\d+(\.\d{0,2})?$/
Which would be a series of digits, potentially followed by a decimal and anywhere between 0 to 2 digits.

Consider using alternation to break down a regular expression into the form a|b|c|d.
Then we can use several different forms, let:
a = 0 -- 0
b = [1-9]\d* -- n (non-zero integer), n cannot start with 0
c = 0[.]\d{1,2} -- 0.x or 0.xy
d = [1-9]\d*[.]\d{1,2} -- n.x or n.xy, n (non-zero integer)
This will allow us to reject values like 09 and 1., as they are not covered by any of the individual forms accepted.

Related

Regular expression of decimal range 0.025 to 99.999

I know that dozens of similar questions was asked before, but cannot find solution which fits my needs.
I need a regular expression as a pattern for validation purposes, which is a range between 0.025 to 99.999 but should also match integers and any possible decimal forms up to 3 decimals:
1 - matched
1.0 - matched
1.000 - matched
0.025 - matched
0.03 - matched
0.01 - not matched
0.024 - not matched
So far my regex looks like this:
^(?!0*(\.0+)?$)([1-9]?[0-9]\.[0-9]{0,3}|[1-9]?[0-9])?$
It actually matches all between 0.001 and 99.999, because managed how to exclude 0 with decimal forms, but don't know how in the easiest way exclude 0.001 to 0.024.
in javascript you can just convert the string to number and use logic like this
const str = '1.0'
const test = +str;
if(test >= 0.025 && test <= 99.999) {
console.log('valid');
}
IMO this will be simpler and easier to read
EDIT:
as mentioned in comments this will pass for strings like 3e-2 which is valid syntax in javascript to define numbers, so you might have to handle that case based on your use case.
To do this with a regular exression, you need something a bit complex:
^(?:0\.02[5-9]|0\.0[3-9]\d?|0\.[1-9]\d{0,2}|[1-9][0-9]?(?:\.\d{1,3})?)$
Here it is in Regex101
Here is how this works in JS
const regex = /^(?:0\.02[5-9]|0\.0[3-9]\d?|0\.[1-9]\d{0,2}|[1-9][0-9]?(?:\.\d{1,3})?)$/;
const testValues = [ "0", "1", "99.999", "0.025", "0.024", "0.01" ];
testValues.forEach(value => console.log(value, regex.test(value)));
Explanation for the expression: it's a bunch of ORs that cover the value range. Although you can compress it more, I've tried to keep it logical and readable:
0\.02[5-9] - values from 0.025 to 0.029
0\.0[3-9]\d? - values from 0.03 to 0.099. The last digit is optional.
0\.[1-9]\d{0,2} - values from 0.1 to 0.999. The last two digits are optional. The fractional part in each case up to now is mandatory, so 0 is not valid.
[1-9][0-9]?(?:\.\d{1,3}) - values from 1 to 99.999. Again, the decimal point and fractional part are set as optional. If present, you can have 1-3 of them.
the entire regex is wrapped in a non-capturing group and then nested between ^ and $ anchors to make sure that the ENTIRE string matches.
Notable exclusions with this regular expression:
exponential form/scientific notation. For example: 3e1 or 3e-1
numbers that lead with zero. For example: 01, 02.345
numbers that start with a sign. For example: +10
values that start from the decimal part. For example: 0.2, 0.123
values that end in a dot. For example 1., 2.
strings that contain whitespace. For example: " 1" will fail to match. The easiest way to solve this is by ensuring the values are trimmed before being tested.

RegEx to filter out all but one decimal point [duplicate]

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery.
Thank you.
Optionally match a + or - at the beginning, followed by one or more decimal digits, optional followed by a decimal point and one or more decimal digits util the end of the string:
/^[+-]?\d+(\.\d+)?$/
RegexPal
The right expression should be as followed:
[+-]?([0-9]*[.])?[0-9]+
this apply for:
+1
+1.
+.1
+0.1
1
1.
.1
0.1
Here is Python example:
import re
#print if found
print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))
#print result
print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))
Output:
True
1.0
If you are using mac, you can test on command line:
python -c "import re; print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))"
python -c "import re; print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))"
You can check for text validation and also only one decimal point validation using isNaN
var val = $('#textbox').val();
var floatValues = /[+-]?([0-9]*[.])?[0-9]+/;
if (val.match(floatValues) && !isNaN(val)) {
// your function
}
This is an old post but it was the top search result for "regular expression for floating point" or something like that and doesn't quite answer _my_ question. Since I worked it out I will share my result so the next person who comes across this thread doesn't have to work it out for themselves.
All of the answers thus far accept a leading 0 on numbers with two (or more) digits on the left of the decimal point (e.g. 0123 instead of just 123) This isn't really valid and in some contexts is used to indicate the number is in octal (base-8) rather than the regular decimal (base-10) format.
Also these expressions accept a decimal with no leading zero (.14 instead of 0.14) or without a trailing fractional part (3. instead of 3.0). That is valid in some programing contexts (including JavaScript) but I want to disallow them (because for my purposes those are more likely to be an error than intentional).
Ignoring "scientific notation" like 1.234E7, here is an expression that meets my criteria:
/^((-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/
or if you really want to accept a leading +, then:
/^((\+|-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/
I believe that regular expression will perform a strict test for the typical integer or decimal-style floating point number.
When matched:
$1 contains the full number that matched
$2 contains the (possibly empty) leading sign (+/-)
$3 contains the value to the left of the decimal point
$5 contains the value to the right of the decimal point, including the leading .
By "strict" I mean that the number must be the only thing in the string you are testing.
If you want to extract just the float value out of a string that contains other content use this expression:
/((\b|\+|-)(0|([1-9][0-9]*))(\.[0-9]+)?)\b/
Which will find -3.14 in "negative pi is approximately -3.14." or in "(-3.14)" etc.
The numbered groups have the same meaning as above (except that $2 is now an empty string ("") when there is no leading sign, rather than null).
But be aware that it will also try to extract whatever numbers it can find. E.g., it will extract 127.0 from 127.0.0.1.
If you want something more sophisticated than that then I think you might want to look at lexical analysis instead of regular expressions. I'm guessing one could create a look-ahead-based expression that would recognize that "Pi is 3.14." contains a floating point number but Home is 127.0.0.1. does not, but it would be complex at best. If your pattern depends on the characters that come after it in non-trivial ways you're starting to venture outside of regular expressions' sweet-spot.
Paulpro and lbsweek answers led me to this:
re=/^[+-]?(?:\d*\.)?\d+$/;
>> /^[+-]?(?:\d*\.)?\d+$/
re.exec("1")
>> Array [ "1" ]
re.exec("1.5")
>> Array [ "1.5" ]
re.exec("-1")
>> Array [ "-1" ]
re.exec("-1.5")
>> Array [ "-1.5" ]
re.exec(".5")
>> Array [ ".5" ]
re.exec("")
>> null
re.exec("qsdq")
>> null
For anyone new:
I made a RegExp for the E scientific notation (without spaces).
const floatR = /^([+-]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)$/;
let str = "-2.3E23";
let m = floatR.exec(str);
parseFloat(m[1]); //=> -2.3e+23
If you prefer to use Unicode numbers, you could replace all [0-9] by \d in the RegExp.
And possibly add the Unicode flag u at the end of the RegExp.
For a better understanding of the pattern see https://regexper.com/.
And for making RegExp, I can suggest https://regex101.com/.
EDIT: found another site for viewing RegExp in color: https://jex.im/regulex/.
EDIT 2: although op asks for RegExp specifically you can check a string in JS directly:
const isNum = (num)=>!Number.isNaN(Number(num));
isNum("123.12345678E+3");//=> true
isNum("80F");//=> false
converting the string to a number (or NaN) with Number()
then checking if it is NOT NaN with !Number.isNaN()
If you want it to work with e, use this expression:
[+-]?[0-9]+([.][0-9]+)?([eE][+-]?[0-9]+)?
Here is a JavaScript example:
var re = /^[+-]?[0-9]+([.][0-9]+)?([eE][+-]?[0-9]+)?$/;
console.log(re.test('1'));
console.log(re.test('1.5'));
console.log(re.test('-1'));
console.log(re.test('-1.5'));
console.log(re.test('1E-100'));
console.log(re.test('1E+100'));
console.log(re.test('.5'));
console.log(re.test('foo'));
Here is my js method , handling 0s at the head of string
1- ^0[0-9]+\.?[0-9]*$ : will find numbers starting with 0 and followed by numbers bigger than zero before the decimal seperator , mainly ".". I put this to distinguish strings containing numbers , for example, "0.111" from "01.111".
2- ([1-9]{1}[0-9]\.?[0-9]) : if there is string starting with 0 then the part which is bigger than 0 will be taken into account. parentheses are used here because I wanted to capture only parts conforming to regex.
3- ([0-9]\.?[0-9]): to capture only the decimal part of the string.
In Javascript , st.match(regex), will return array in which first element contains conformed part. I used this method in the input element's onChange event , by this if the user enters something that violates the regex than violating part is not shown in element's value at all but if there is a part that conforms to regex , then it stays in the element's value.
const floatRegexCheck = (st) => {
const regx1 = new RegExp("^0[0-9]+\\.?[0-9]*$"); // for finding numbers starting with 0
let regx2 = new RegExp("([1-9]{1}[0-9]*\\.?[0-9]*)"); //if regx1 matches then this will remove 0s at the head.
if (!st.match(regx1)) {
regx2 = new RegExp("([0-9]*\\.?[0-9]*)"); //if number does not contain 0 at the head of string then standard decimal formatting takes place
}
st = st.match(regx2);
if (st?.length > 0) {
st = st[0];
}
return st;
}
Here is a more rigorous answer
^[+-]?0(?![0-9]).[0-9]*(?![.])$|^[+-]?[1-9]{1}[0-9]*.[0-9]*$|^[+-]?.[0-9]+$
The following values will match (+- sign are also work)
.11234
0.1143424
11.21
1.
The following values will not match
00.1
1.0.00
12.2350.0.0.0.0.
.
....
How it works
The (?! regex) means NOT operation
let's break down the regex by | operator which is same as logical OR operator
^[+-]?0(?![0-9]).[0-9]*(?![.])$
This regex is to check the value starts from 0
First Check + and - sign with 0 or 1 time ^[+-]
Then check if it has leading zero 0
If it has,then the value next to it must not be zero because we don't want to see 00.123 (?![0-9])
Then check the dot exactly one time and check the fraction part with unlimited times of digits .[0-9]*
Last, if it has a dot follow by fraction part, we discard it.(?![.])$
Now see the second part
^[+-]?[1-9]{1}[0-9]*.[0-9]*$
^[+-]? same as above
If it starts from non zero, match the first digit exactly one time and unlimited time follow by it [1-9]{1}[0-9]* e.g. 12.3 , 1.2, 105.6
Match the dot one time and unlimited digit follow it .[0-9]*$
Now see the third part
^[+-]?.{1}[0-9]+$
This will check the value starts from . e.g. .12, .34565
^[+-]? same as above
Match dot one time and one or more digits follow by it .[0-9]+$

Regex for validating currency number format

I've got following formats, that are acceptable
1200000,00
1200000.00
1,200,000.00
1 200 000.00
1 200 000,00
1 200 000,0000
-1 200 000.00
At the moment I was able to verify only ^-?\\d+$, ^-?\\d+[\\,\\.]\\d{2}$, ^-?\\d+[\\,\\.]\\d{2,}$. Two last format are separate, so that I would know is rounding needed or not. All three format use gm flags to check string from start ^ to end $.
Those regular expressions cover only first two elements in list. Other elements, that use commas and spaces for thousand separation are not verified yet and I'm not sure how to achieve that.
Also there is a "beautifier" expression (\\d)(?=(\\d{3})+(?!\\d)), that will take this 1200000,00 and turn it into 1 200 000,00 with such usage '1200000,00'.replace(('(\\d)(?=(\\d{3})+(?!\\d))', 'g'), '$1 ').
So question states, what would be a correct regular expression to validate such format 1 200 000.00 or 1,200,000.00? Since I assume difference with \s\, could be easily done in same expression.
Thank you.
For validating the last two numbers, you can use the following:
^-?\d{1,3}(?:[\s,]\d{3})*(?:\.\d+)?$
1 2 3 4 5
Optional minus sign
1..3 digits
Zero or more fragments that consist of
comma or space
3 digits
optional fraction part consisting of a dot followed by 1 or more digits.
This doesn't directly solve the problem due to me misreading. But it might still be useful to someone so I'll let it stay.
Stop trying to solve every problem with regex. Regex is great when you have one or two very well defined strings. Not a million formats.
This can be solved with minimal regex. Magic is in the bold part.
var numbers = [
"1200000,00",
"1200000.00",
"1,200,000.00",
"1 200 000.00",
"1 200 000,00",
"1 200 000,0000",
"-1 200 000.00"
];
var parseWeirdNumber = function(numberString) {
//Split numbers to parts. , . and space are all valid delimiters.
var numberParts = numberString.split(/[.,\s]/);
//Remove the last part. **This means that all input must have fraction!!**
var fraction = numberParts.pop();
//Rejoin back without delimiters, and reapply the fraction.
//parseFloat to convert to a number
var number = parseFloat(numberParts.join('') + "." + fraction);
return number;
}
numbers = numbers.map(parseWeirdNumber);
console.log(numbers);

Need a Reg Expression to find a decimal range

I am currently needing a reg expression which will evaluate a decimal range.
The requirements are as below
1) Can allow only 1 or 2 decimal places after dot or can as well allow whole numbers (e.g) 1234 , 123.4, 1245.78 are valid
2) The range should be within 9999 (e.g) 9999.0 , 9998.99 , 9999.00 - Valid | 9999.01,10000.00 - not Valid
3)Do not require leading or trailing zeros
So far i have tried to achieve till writing this reg expression
/^[0-9]\d{1,4}(\.\d{1,2})?$/.test(value);
... but unable to proceed with setting range till digit 9999 (since 9999.01 also not valid )can you help.
Why don't just apply regular expression to determine is your string a valid digit with dots float, then typecast it to Number and find wether it is bigger than 9999 or not.
Regexp for your needs caould be very complex and take too much CPU from client.
Here's something quick and dirty that should work for you: http://regex101.com/r/vK1jM3
/^(?(?=9999)9999(?:\.0+)?|\d{1,4}(?:\.\d{1,2})?)$/gm
I merely handle the special case of 9999
This works as far as I can see:
^(9999(?!\.[1-9])(?!\.0[1-9])\.[0-9]{1,2}|9999|(?!9999)[0-9]{1,4}|(?!9999)[0-9]{1,4}\.[0-9]{1,2})$
Testing it out:
var monstrosity = /^(9999(?!\.[1-9])(?!\.0[1-9])\.[0-9]{1,2}|9999|(?!9999)[0-9]{1,4}|(?!9999)[0-9]{1,4}\.[0-9]{1,2})$/;
console.log(monstrosity.test("9999.00")); // true
console.log(monstrosity.test("9999.01")); // false
console.log(monstrosity.test("9999")); // true
console.log(monstrosity.test("9998.4")); // true
console.log(monstrosity.test("0")); // true
console.log(monstrosity.test("0.5")); // true
If you add something like this to the codebase, future maintenance programmers will hunt you down with pitchforks. Try to solve the range check without regex, as webbandit suggested.
Why a regexp? Just do
x > 0 && x <= 9999 && (x*100 - Math.floor(x*100) == 0)

Preventing concatenation

I've been writing JavaScript on and off for 13 years, but I sort of rediscovered it in the past few months as a way of writing programs that can be used by anyone visiting a web page without installing anything. See for example.
The showstopper I've recently discovered is that because JavaScript is loosely typed by design, it keeps concatenating strings when I want it to add numbers. And it's unpredictable. One routine worked fine for several days then when I fed different data into it the problem hit and I ended up with an impossibly big number.
Sometimes I've had luck preventing this by putting ( ) around one term, sometimes I've had to resort to parseInt() or parseFloat() on one term. It reminds me a little of trying to force a float result in C by putting a .00 on one (constant) term. I just had it happen when trying to += something from an array that I was already loading by doing parseFloat() on everything.
Does this only happen in addition? If I use parseInt() or parseFloat() on at least one of the terms each time I add, will that prevent it? I'm using Firefox 6 under Linux to write with, but portability across browsers is also a concern.
The specification says about the addition operator:
If Type(lprim) is String or Type(rprim) is String, then
Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
Which means that if at least one operator is a string, string concatenation will take place.
If I use parseInt() or parseFloat() on at least one of the terms each time I add, will that prevent it?
No, all operands have to be numbers.
You can easily convert any numerical string into a number using the unary plus operator (it won't change the value if you are already dealing with a number):
var c = +a + +b;
I normally do this:
var x = 2;
var t = "12";
var q = t+x; // q == "122"
var w = t*1+x; // *1 forces conversion to number w == 14
If t isn't a number then you'll get NaN.
If you multiply by 1 variables you don't know what type they are. They will be converted to a number. I find this method better than doing int and float casts, because *1 works with every kind of numbers.
The problem you are having is that the functions which fetch values from the DOM normally return strings. And even if it is a number it will be represented as a string when you fetch it.
You can use + operator to convert a string to number.
var x = '111'
+x === 111
Rest assured it is very predictable, you just need to be familiar with the operators and the data types of your input.
In short, evaluation is left-to-right, and concatenation will occur whenever in the presence of a string, no matter what side of the operation.
So for example:
9 + 9 // 18
9 + '9' // '99'
'9' + 9 // '99'
+ '9' + 9 // 18 - unary plus
- '9' + 9 // 0 - unary minus
Some ternary expressions:
9 + '9' + 9 // '999'
9 + 9 + '9' // '189'

Categories

Resources