geevee
geevee

Reputation: 5451

Javascript regular expression for english & numeric characters only

What is the easiest way to check if an expression has English or number characters only? with no spaces and no other characters.

p.s - the first character cannot be a number. upper or lower case.

Upvotes: 10

Views: 46543

Answers (6)

MohammadSoori
MohammadSoori

Reputation: 2408

/^[A-Za-z\d]+$

/^[A-Za-z\d]+$.test("TEST1"); // true
/^[A-Za-z\d]+$.test("Test2"); // true
/^[A-Za-z\d]+$.test("test3"); // true 
/^[A-Za-z\d]+$.test("4TEST"); // true
/^[A-Za-z\d]+$.test("5Test"); // true
/^[A-Za-z\d]+$.test("6test"); // true 
/^[A-Za-z\d]+$.test("TE7ST"); // true
/^[A-Za-z\d]+$.test("Te8st"); // true
/^[A-Za-z\d]+$.test("te9st"); // true 

Upvotes: 3

Radagast the Brown
Radagast the Brown

Reputation: 3346

Easiest:

/^[a-z][a-z0-9]*$/i

explanation of the expression:

  • / - open expression
  • ^ - string must start here. Nothing before
  • [a-z] - find only one character between a to z, including
  • [a-z0-9]* - find any sequence of characters either between a to z including, or between 0-9 including (the "any sequence" part is the * in the end)
  • $ - string must end here. Nothing after
  • / - close expression
  • i - the expression is case insensitive

tested with the following cases

var tests = //key = case, value = expected results { "joe" : true //only lower case , "JOE" : true //only capital , "charsAndCaps" : true //mixed case , "ABC444" : true //caps and numbers , "AAaaAA3276" : true //mixed case with numbers , "111Joe" : false //starts with number , "112345" : false //only numbers , "asaaa$" : false //non-alphanumeric char in the end , "asaaaלא" : false //non-latin char in the end , "asaaнет" : false //non-latin char in the end , "#asaaa" : false //non-alphanumeric char in the start , "לאasaaa" : false //non-latin char in the start , "нетasaa" : false //non-latin char in the start , "aaלאasaa" : false //non-latin char in the middle , "sssнетaa" : false //non-latin char in the middle , "as&&aaa" : false //non-alphanumeric char in the middle , "" : false //empty string }

try it at: http://jsfiddle.net/erJ4H/161/

Upvotes: 14

Grzegorz Gierlik
Grzegorz Gierlik

Reputation: 11232

I would use: /^[A-Za-z][A-Za-z0-9]*$/. Here are same examples:

/^[A-Za-z][A-Za-z0-9]*$/.test("expression");
/^[A-Za-z][A-Za-z0-9]*$/.test("EXPRESSION");
/^[A-Za-z][A-Za-z0-9]*$/.test("e123xpression");
/^[A-Za-z][A-Za-z0-9]*$/.test("E123xpression");
/^[A-Za-z][A-Za-z0-9]*$/.test("1expression");

Without boundaries (^ and $) regexp match any substring too.

EDIT: Updated invalid expression

Upvotes: 16

Maxym
Maxym

Reputation: 11896

try this one:

/^[a-z][a-z\d]*$/i

add some examples:

/^[a-z][a-z\d]*$/i.test("check#$#"); // false
/^[a-z][a-z\d]*$/i.test("1check"); // false
/^[a-z][a-z\d]*$/i.test("check1"); // true 
/^[a-z][a-z\d]*$/i.test("cHEck1"); // true

Upvotes: 3

xkeshav
xkeshav

Reputation: 54022

try this /^[a-z]+[a-z0-9]*$/i

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328604

Use this: [a-zA-Z][a-zA-Z0-9]*

Upvotes: 2

Related Questions