crazy novice
crazy novice

Reputation: 1817

only allow white space in between characters

I only want to allow a-z, A-Z and space in between characters. For this I am using following regex, problem is that it allows the leading and trailing spaces also. Can this be adjusted to not allow leading or trailing white space.

[a-zA-Z\\ \\\']*

examples 'prince charles' should pass
' prince charles' should fail due to leading whitespace
'prince charles ' should fail due to trailing whitespace

Upvotes: 1

Views: 1469

Answers (2)

pguardiario
pguardiario

Reputation: 54984

Word boundaries. It's as simple as that:

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

Upvotes: 2

Rohit Jain
Rohit Jain

Reputation: 213203

To dis-allow leading and trailing whitespace, you can use:

^[a-zA-Z](?:[a-zA-Z ]*[a-zA-Z])?$

Upvotes: 3

Related Questions