0

I have a form that use a regular expression that validate decimals and natural numbers, but I have a problem, this doesn't validate empty fields. My expression regular is this :

"^([0-9])+\.[0-9]|[0-9]$"

what can I do in order to this regular expression validate empty fields? thanks.

Jeason
  • 1
  • 2

4 Answers4

3

Use this regular expression to handle both number(only positive) and empty.

^(?:[0-9]+(?:\.[0-9]+)?)?$

Explanation of the regex is:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
      [0-9]+                   any character of: '0' to '9' (1 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Thanks guys, I proved all of your suggestions but It not resolved my problem at all, still allow to enter empty fields, I maybe not explain very well, I would like that my regular expression does not permit to register empty fields. – Jeason Mar 26 '14 at 20:31
  • @Jeason you are right, you didn't explain it well. We got you mean to allow empty and that caused the problem. So, the fix is just remove the `?` before the `$`. Finally it will be `^(?:[0-9]+(?:\.[0-9]+)?)$` and it will not allow any empty. – Sabuj Hassan Mar 27 '14 at 07:46
1

Do you mean this?

"^(?:([0-9])+\.[0-9]*|[0-9]|)$"

This regex contains an additional possibility of no characters.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

Try:

^([0-9]+\.[0-9]*|[0-9]*)$

This:

  • fixes the problem that only exactly 1 digit was accepted after the decimal dot,
  • fixes the problem you had that you only accepted natural numbers of a single digit,
  • fixes the problem that the beginning and end of the field were not matched in both cases,
  • allows 0 digits, which is an empty field.
0

Thanks guys,

I proved all of your suggestions but It not resolved my problem at all, still allow to enter empty fields, I maybe not explain very well, I would like that my regular expression does not permit to register empty fields.

Jeason
  • 1
  • 2