Reputation: 1
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
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
Reputation: 20163
Do you mean this?
"^(?:([0-9])+\.[0-9]*|[0-9]|)$"
This regex contains an additional possibility of no characters.
Upvotes: 1
Reputation: 474
Try:
^([0-9]+\.[0-9]*|[0-9]*)$
This:
Upvotes: 0
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