regex allow only numbers or empty string

You’re looking for:

/^(\s*|\d+)$/

If you want a positive number without leading zeros, use [1-9][0-9]*

If you don’t care about whitespaces around the number, you can also try:

/^\s*\d*\s*$/

Note that you don’t want to allow partial matching, for example 123abc, so you need the start and end anchors: ^...$.
Your regex has a common mistake: ^\s*|\d+$, for example, does not enforce a whole match, as is it the same as (^\s*)|(\d+$), reading, Spaces at the start, or digits at the end.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)