Comma separated words Regex (html input pattern)

Viewed 661

im trying to force the user to input comma seperated words and reject any white space seperated words using the pattern attribute thats part of the HTML5 <input/> tag .

My following Expression expression doesn't work even though this says it does when I plug it in and test it.

My expression : ^((([a-zA-Z]\s*)+)\, ?(([a-zA-Z]\s*)+))$

<div class="col-md-6">

    <input type="text" id="traits" name = "traits" class="form-control"  placeholder = "crazy, hyper, outgoing" pattern="^((([a-zA-Z]\s*)+)\, ?(([a-zA-Z]\s*)+))$" required>

</div>
1 Answers

This allows only comma separated words:

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

No spaces allowed, no more than one comma at a time, and commas should be only between words.

Demo

Related