user1288707
user1288707

Reputation:

Parameters validation with Express using Regular Expressions

I would like to validate http request parameters in Express (and express-param) using regular expressions. This parameter must be a number with 11 digits at least.

Why app.param('uid', /^[0-9]{11,}+$/); returns an error?

And why app.param('uid', /^[0-9]{11,}/); don't works fine? It blocks params like 1234567890c and accepts 12345678901c or 12345678901ca.

Upvotes: 0

Views: 1170

Answers (1)

newfurniturey
newfurniturey

Reputation: 38446

Your two patterns are inconsistent. The first (which is an invalid regex) has an ending $ specifying that the input must match your pattern up to the end of the string. The problem is you have a + after the numbers which causes it to be invalid as you specify the number of times it should repeat already with {11,}.

The second pattern drops both the + and the $. Dropping the + is perfect, however, when you drop the $ you say that anything after the numbers can appear.

Try adding a $ to the second pattern:

app.param('uid', /^[0-9]{11,}$/);

Upvotes: 1

Related Questions