How to allow numbers only using f:validateRegex

2020-08-21 02:22发布

问题:

I've an input which should allow numbers only. I tried adding <f:validateRegex> as follows:

<f:validateRegex pattern="[0-9]" />

But I still get the error message,

Validation Error: Value not according to pattern '[0-9]'

How is this caused and how can I solve it?

回答1:

The pattern [0-9] allows input of a single digit only. Perhaps you want input of multiple digits? In that case, you should use a pattern of [0-9]+. The + modifier namely means "one or more".

<f:validateRegex pattern="[0-9]+" />

If you allow empty input, then use the * modifier instead which means "zero or more".

<f:validateRegex pattern="[0-9]*" />

See also:

  • regular-expressions.info - the premier website about regular expressions