July 26th, 2010, 03:34 PM
Allowing a decimal OR whole number
I have the following RegEx and cant figure out how to adapt it to suit my needs.
Currently it will allow a number between 1-100
However I want it to accept a number between 1-100 either as a whole number or a decimal.
The code in bold is what I need to adapt..
var discountRegEx = /^([a-zA-Z]+\d?)-(5[0-2]|[1-4]\d|[1-9])-(100|[1-9]\d|[1-9])$/i;
Please any advice would be much appreciated.
Thanks in advance,
July 26th, 2010, 06:02 PM
How about (untested):
That won't allow 100.0 though: do you want that too.
Essentially the second part is:
[1-9] - a character between 1 and 9
\d? - optionally followed by another digit
(?: - start a non-capturing group
\.\d+ - a decimal point followed by one or more digits
) ?- close the non-capturing group and make matching its contents optional
July 27th, 2010, 01:36 PM
Thats perfect you are a legend! I find ReqEx so difficult so this is much appreciated!
Originally Posted by ishnid
I adapted it so that I could enter 0. something
July 27th, 2010, 01:48 PM
Can I ask what you do for a living?
Just wondering if there are jobs specific to RegEx or are you a programmer of some sort. If so which language(s)
July 28th, 2010, 07:21 AM
You can! I'm an academic, pursuing a PhD.
Originally Posted by JoeSad
July 28th, 2010, 09:04 AM
There is no reason to use regular expressions for this at all, except as a thought exercise. Convert the input to a number, and then check if it's greater than zero and less than 100.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question
or you're a Help Vampire.
Trying to argue intelligently? Please read this.