Forum Moderators: open

Message Too Old, No Replies

Issue in regular expression

         

Gowri pandiyan

11:48 am on May 10, 2016 (gmt 0)

10+ Year Member



Hi Gurus,

I would like to validate an input in the following format (+/-)XXXXX.XXXX or XXXXX.XXXX or XXXXX i.e., Allowing maximum 5 digits before period, maximum 4 digits after period, value after period is optional and symbols +/- is also optional. Below is the pattern I have tried so far, but it isn't working.

/^[-+]?[0-9]{1-5}+\.?([0-9]{0-4}*)?$/

Please let me know what needs to be done in the above code.

Best regards,
Gowri

Gowri pandiyan

12:01 pm on May 10, 2016 (gmt 0)

10+ Year Member



/^[-+]?[0-9]{1,5}\.?([0-9]{0,4})?$/

I have modified the code to above, It seems to be correct to me. Please let me know if I can go ahead and use the above code.

lucy24

8:11 pm on May 10, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A special quirk about the - sign is that it has special meaning within grouping brackets. (Assuming that's an ordinary keyboard hyphen, and not a special ndash character or hard hyphen or whatnot.) The RegEx engine may make exceptions if it's at the very beginning or very end of the grouping brackets; to be safe, escape it as [+\-] or [\-+].

My personal preference is for \d instead of [0-9] but really that's about individual preference; I really doubt there's any significant difference in performance.

format (+/-)XXXXX.XXXX or XXXXX.XXXX or XXXXX i.e., Allowing maximum 5 digits before period, maximum 4 digits after period

If I were facing the question from scratch, I'd say
[+-]?\d{1,5}(\.{0,4})?
Note position of (parentheses)?

Does this expression come in isolation, for example as the entire content of an input field? If so then yes, use the anchors
^[+-]?\d{1,5}(\.{0,4})?$
If it can also come in mid-text, then things get more complicated. I assume you've already got code to strip away leading and trailing blanks, including spurious line breaks.

{1-5}+ <snip> {0-4}*

I know the RegEx engine in JavaScript has some quirks, but to me this just looks like a mistake. Either +* or {number-range in brackets} but not both; they're mutually exclusive.

Could there legitimately be 0 digits before the period, like ".314"? If so, you need to add the option for \.\d{1,4} alone, non-optional. But we won't worry about that unless we have to.

Gowri pandiyan

5:31 am on May 11, 2016 (gmt 0)

10+ Year Member



Thanks a lot for your suggestions which would really help me improvise my code!

Gowri pandiyan

10:58 am on May 11, 2016 (gmt 0)

10+ Year Member



Thanks Lusy for your help! I have one more doubt on regression pattern mentioned below.

/^\d{5}$/

I don't want to allow "00000" to be entered while restricting 5 digits with above code. Is there any way to restrict it?

lucy24

6:36 pm on May 11, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can get rid of leading zeros either by pre-editing the input (ignore leading/trailing zeros* just as you'd ignore leading/trailing whitespace) or within the Regular Expression:

^0*[1-9]\d{0,4}(\.\d{0,4})?$
or
^0*[1-9]\d{0,4}$

(depending on whether you've still got the four-decimal-places business, or something else now). This means: Ignore leading zeros, if any; one non-zero character; from 0 to 4 additional numeric characters with any value. The total input length might be more than 5 characters, but I don't suppose it matters, as long as the significant part is no more than 5 total. (If it does matter, it gets more complicated, because there will be five possible patterns.)


* Yes, I realize that "2.5" is not necessarily the same thing as "2.5000". It all depends on context.