Jeason
Jeason

Reputation: 1

Regular Expression to validate empty fields

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.

Upvotes: 0

Views: 2308

Answers (4)

Jeason
Jeason

Reputation: 1

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.

Upvotes: 0

aliteralmind
aliteralmind

Reputation: 20163

Do you mean this?

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

This regex contains an additional possibility of no characters.

Upvotes: 1

Klaas van Aarsen
Klaas van Aarsen

Reputation: 474

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.

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

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

Upvotes: 3

Related Questions