adam84 Posted July 15, 2008 Share Posted July 15, 2008 This is my expression /^([a-zA-Z0-9_.-])+/; What I want is to accept 1. letters 2. numbers 3. spaces 4. underscores When I tested the expression above, it didnt work Any ideas? Quote Link to comment Share on other sites More sharing options...
dsaba Posted July 15, 2008 Share Posted July 15, 2008 The whole string must be that, or part of the string? part of string: \[a-zA-Z0-9_\.\-]+\ the whole string: \^[a-zA-Z0-9_\.\-]+$\m http://is.gd/Tyt why the subgroup? it looks fine, its probably something else that is causing your problems. You should have posted some problem code. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 15, 2008 Share Posted July 15, 2008 function validString(string) { var invalidChars = new RegExp(/[^\w ]/); return !invalidChars.test(string); } It's easier to check for invalid characters than to test for valid characters. \w = Match any alphanumeric character including the underscore. Equivalent to [a-zA-Z0-9_]. So using that and adding a space check for any character that is not in the approved "white" list. Quote Link to comment 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.