Soul Slayer
Soul Slayer

Reputation: 297

Pattern matching for characters, comma and semicolon

I am trying to pattern match against a textbox input field.

The entered text should consist of only characters separated by a ',' or a ';' followed by an optional space.

Example: Jane, Doe, Jane,Doe, Jane,Doe; Jack,Black and Jane,Doe;Jack,Black are valid. But Jane Doe, (leading space)Jane and Doe(trailing space) are invalid.

The current pattern I have is, /^[A-Za-z]+(,;)?$/

Please help.

Upvotes: 0

Views: 718

Answers (2)

ganzogo
ganzogo

Reputation: 2594

How about this:

/^[A-Za-z]+([,;]\s?[A-Za-z]+)*$/

Upvotes: 3

hsz
hsz

Reputation: 152206

Try with following regex:

/^[a-zA-Z]+([,;] ?[a-zA-Z]+)*$/

Upvotes: 2

Related Questions