javascirpt validation
-
Dear all, i need to do a javascript validation for a textbox, this textbox is for the amount, the user can enter for example the value: 2 or 3.2 or 0.7 I should allow him to enter a digit and also ".", what is the simplest regular expression to do this. The developer that worked previously on this project, put this regular expression: var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; I can't understand it specially the -? in the begining of each part, what exactly do the "-?" and also for which validation this regular expression is used. Thanks.
-
Dear all, i need to do a javascript validation for a textbox, this textbox is for the amount, the user can enter for example the value: 2 or 3.2 or 0.7 I should allow him to enter a digit and also ".", what is the simplest regular expression to do this. The developer that worked previously on this project, put this regular expression: var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; I can't understand it specially the -? in the begining of each part, what exactly do the "-?" and also for which validation this regular expression is used. Thanks.
The -? means a minus (-) or none minussign. I think the most simple regexp is:
(^-?\d+(\.\d*)?$)|(^-?\d*(\.\d+)?$)
This will allow also number like .7 and -.3 A little simpler is:^-?\d+(\.\d*)?$
This does not allow .7 or -.3 but it will allow 0.7 and -0.3 Wout Louwers -- modified at 7:39 Thursday 8th June, 2006