0

I found this code for alphanumeric check ("Letters, numbers, spaces or underscores") but I want to change so I will be able to write only letters or numbers. Can anyone tell me how to change this code:

function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);}

Thanks!

Greg

Greg
  • 3
  • 3
  • 1
    Aha, a password weakener. Why not just five radio buttons to select from the preselected passwords "1234", "asdf", "qwerty", "999" and "password"? ;) – Guffa Mar 29 '10 at 12:39
  • 1
    Or, if you thought about it for a moment, you could stop being sarcastic and realise that you have no idea of the context, and that there could be any number of application-specific data items that should be solely alphanumeric. – Matt Sach Apr 27 '10 at 14:36

2 Answers2

2

This should do it:

function(value, element)
{
    return this.optional(element) || /^[a-zA-Z0-9]+$/i.test(value);
}

EDIT: OK, no idea why I can't get that formatting nicely, but the important part is replacing the \w with the character class (the bit in []).

Matt Sach
  • 1,162
  • 16
  • 37
  • 1
    Four-space indenting for code works a lot better than the back ticks unless you want the code fragment to be inline with your text. – tvanfosson Mar 29 '10 at 12:01
  • Oh, and the `i` at the end of the regex means "case insensitive" so you don't need the lower (or upper, if you choose) case letters in the bracket expression -- or you could drop the "insensitive" flag. – tvanfosson Mar 29 '10 at 12:01
  • Ach, four-space indenting, that's the puppy! And good point about the case-insensitivity. – Matt Sach Mar 29 '10 at 12:03
0

I've used this for years now :

http://www.itgroup.com.ph/alphanumeric/

Take a look

ant
  • 22,634
  • 36
  • 132
  • 182