REGEx Price Explained
REGEx Price Explained
“/(^$)|(^[0-9]+$)|(^[0-9]+\.[0-9][0-9]$)/”,
This piece of REGEX has three different options as you can see with the ” | ” thingy which stands for “or”.
Option #1 is the (^$). The “^” means “Start with whatever is after this. The “$” means “end with whatever is before this. Since there is nothing after the “^” and nothing before the “$”, this piece of REGEX will allow “” (empty) through.
Option #2 is the (^[0-9]+$). The “^” means “start with this. [0-9] means any number. “+” means “for one more more times”. The “$” means “ends with this”. So, in English, this basically says “start and end with more or more numbers”. This will return true with “7″ or with “32553″. It will not return true with “32,553″ because there is a comma in there. There are a few ways to ditch the comma. I use a little piece of javascript code within my text field like this, REGEX Remove Commas From Form Textfield.
Option #3 is (^[0-9]+\.[0-9][0-9]$). The “^” says “start with this”. We have a [0-9] thingy so that means “any number” and the “+” means “at least one time”. So we could match one or a zillion numbers. Then we have “\.”. The “\” is for “literals”. The “.” without the literal thingy will match anything (letters, numbers, spaces, characters, etc) but using the “\.” method we are only going to match a single period. After that we have [0-9] again. Notice there is no “+” like we used in the first half of this option. Instead, I opted to use a second [0-9]. This is because I only want to match 2 digits after the period. Then we have the “$” which says “end with me”. So, in English, this REGEX says “Start with at least one number, make sure there is a period, and then make sure sure it ends with 2 digits.