johnrb87 Posted July 9, 2011 Share Posted July 9, 2011 Hi I have the following allowed in my regex validation var regex = /[a-zA-Z0-9]+/; how can I extend that to allow hyphens, underscores and spaces only? thanks Quote Link to comment https://forums.phpfreaks.com/topic/241480-regex-validation/ Share on other sites More sharing options...
.josh Posted July 9, 2011 Share Posted July 9, 2011 Just add them into the character class with the rest of the stuff. Only real trick is the hyphen, since it is also used to specify ranges. You can put it anywhere in the list if you escape it first (preceding it with a \) but you can also put it as the first or last item. Also, since you want to only allow those chars for the whole string, you need to include start and ending string anchor tags. Otherwise, it will match or not match based on substrings withing the string. var regex = /^[-a-zA-Z0-9_ ]+$/; Quote Link to comment https://forums.phpfreaks.com/topic/241480-regex-validation/#findComment-1240545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.