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? Link to comment https://forums.phpfreaks.com/topic/114766-javascript-regex-help/ 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. Link to comment https://forums.phpfreaks.com/topic/114766-javascript-regex-help/#findComment-590147 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. Link to comment https://forums.phpfreaks.com/topic/114766-javascript-regex-help/#findComment-590207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.