Regular Expression Which Allow Only Characters or Space

Regular expression which allow only characters or space

you can use this

/^([a-zA-Z]+\s)*[a-zA-Z]+$/

regex to allow only alphabets and no special characters, with no space at the beginning

First of all, you missed the beginning of string anchor ^.

Secondly, the inverse character class [^-\s] is a good attempt at not allowing spaces, but at the same time it does allow other special characters to be at the beginning of the string, which we do not want. Instead, we could just something very similar to the second character class, just without the \s: [a-zA-Z].

Full regex:

^[a-zA-Z][a-zA-Z\s]*$

javascript regex allow only letters . - and spaces. And all should be from 2 to 256 symbols

You can use this regex:

var regex = /^[a-z][a-z.\s-]{1,255}$/i;


Related Topics



Leave a reply



Submit