I'm currently using this regex ^[A-Z0-9 _]*$
to accept letters, numbers, spaces and underscores. I need to modify it to require at least one number or letter somewhere in the string. Any help would be appreciated!
This would be for validating usernames for my website. I'd actually like to support as many characters as I can, but just want to ensure that I prevent code injection and that characters will display fine for all users. So I'm definitely open to regex validation suggestions that would support a wider set of characters.
I'm currently using this regex ^[A-Z0-9 _]*$
to accept letters, numbers, spaces and underscores. I need to modify it to require at least one number or letter somewhere in the string. Any help would be appreciated!
This would be for validating usernames for my website. I'd actually like to support as many characters as I can, but just want to ensure that I prevent code injection and that characters will display fine for all users. So I'm definitely open to regex validation suggestions that would support a wider set of characters.
Share Improve this question edited Aug 12, 2022 at 18:58 spazm 4,7891 gold badge35 silver badges34 bronze badges asked Feb 23, 2009 at 0:43 makeeemakeee 2,8055 gold badges36 silver badges42 bronze badges 4- i'ts stripping out the underscore for some reason, but you get the point.. – makeee Commented Feb 23, 2009 at 0:45
- @makeee mark the regex as code (surround it with ` characters) and you'll be okay. – Daniel LeCheminant Commented Feb 23, 2009 at 0:49
- Or do what I did, escape the underscore with a "\". – paxdiablo Commented Feb 23, 2009 at 0:50
- @Pax/Makeee: Can one of you re-title it so it says "at least one letter" – Daniel LeCheminant Commented Feb 23, 2009 at 0:53
8 Answers
Reset to default 85You simply need to specify your current RE, followed by a letter/number followed by your current RE again:
^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$
Since you've now stated they're Javascript REs, there's a useful site here where you can test the RE against input data.
If you want lowercase letters as well:
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
To go ahead and get a point out there, instead of repeatedly using these:
[A-Za-z0-9 _]
[A-Za-z0-9]
I have two (hopefully better) replacements for those two:
[\w ]
[^\W_]
The first one matches any word character (alphanumeric and _
, as well as Unicode) and the space. The second matches anything that isn't a non-word character or an underscore (alphanumeric only, as well as Unicode).
If you don't want Unicode matching, then stick with the other answers. But these just look easier on the eyes (in my opinion). Taking the "preferred" answer as of this writing and using the shorter regexes gives us:
^[\w ]*[^\W_][\w ]*$
Perhaps more readable, perhaps less. Certainly shorter. Your choice.
EDIT:
Just as a note, I am assuming Perl-style regexes here. Your regex engine may or may not support things like \w and \W.
EDIT 2:
Tested mine with the JS regex tester that someone linked to and some basic examples worked fine. Didn't do anything extensive, just wanted to make sure that \w and \W worked fine in JS.
EDIT 3:
Having tried to test some Unicode with the JS regex tester site, I've discovered the problem: that page uses ISO instead of Unicode. No wonder my Japanese input didn't match. Oh well, that shouldn't be difficult to fix:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Or so. I don't know what should be done as far as JavaScript, but I'm sure it's not hard.
^[ _]*[A-Z0-9][A-Z0-9 _]*$
You can optionally have some spaces or underscores up front, then you need one letter or number, and then an arbitrary number of numbers, letters, spaces or underscores after that.
Something that contains only spaces and underscores will fail the [A-Z0-9]
portion.
You can use a lookaround:
^(?=.*[A-Za-z0-9])[A-Za-z0-9 _]*$
It will check ahead that the string has a letter or number, if it does it will check that the rest of the chars meet your requirements. This can probably be improved upon, but it seems to work with my tests.
UPDATE:
Adding modifications suggested by Chris Lutz:
^(?=.*[^\W_])[\w ]*$/
for me @"^[\w ]+$" is working, allow number, alphabet and space, but need to type at least one letter or number.
This will validate against special characters and leading and trailing spaces:
var strString = "Your String";
strString.match(/^[A-Za-z0-9][A-Za-z0-9 ]\*[A-Za-z0-9]\*$/)
$("#ValuationName").bind("keypress", function (event) {
if (event.charCode!=0) {
var regex = new RegExp("^[a-zA-Z ]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
});
Simply u can add this to jquery.validationEngine-en.js file
"onlyLetterNumberSp": {
"regex": ^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$,
"alertText": "* No special characters allowed"
},
and call it in text field as
<input type="text" class="form-control validate[required,custom[onlyLetterNumberSp]]" id="title" name="title" placeholder="Title"/>