Erfan
Erfan

Reputation: 113

How to allow numbers only using f:validateRegex

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?

Upvotes: 9

Views: 24946

Answers (1)

BalusC
BalusC

Reputation: 1108742

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:

Upvotes: 20

Related Questions