html5 - Html 5 regex that allow space between characters -


i need regexp fails in every character outside [a-za-z0-9_& -]

space between characters allowed, not empty spaces.

allowed input examples:

group1, group 1, group1, group1 & group2, group1_group2, group1_group2

my html input type :

<input type="text" required name="name" pattern="[a-za-z0-9_& -]+" placeholder="group name" />

the problem single empty space, if put single empty space, pass. if remove space in regex pattern , become: "[a-za-z0-9_&-]+", user won't able input space between string, group 1, how should form pattern?

try regex:

^(?!^ +$)([\w -&]+)$ 

click here demo

explanation:

  • ^ - start of string
  • (?!^ +$) - negative lookahead validating string should not contain 1 or more spaces between start , end of string
  • ([\w -&]+) - capturing 1 or more characters in range [a-za-z0-9_ &-]
  • $ - end of string

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -