not allow space in regex code example
Example 1: regular expression not to allow space in javascript
var regexp = /^\S*$/; // a string consisting only of non-whitespaces
Example 2: space allow in regex
Just add a space in your character class.
^[a-zA-Z0-9_ ]*$