0

I need to validate a form field for a specific type of combination of numbers and alphabets:

  • first four digits are alphabets
  • next digit is zero
  • next 6 digits are numeric

e.g.
IBKL 0 001084

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Max Ahmed
  • 135
  • 1
  • 8
  • So what is the problem you have? Are you asking somebody to write an entire form validation function for you, or do you just need help with a quick regex for your field? `/^[A-Z]{4} 0 \d{6}$/` (By the way, "alphabet" doesn't mean what you think it means. You mean "letter".) – nnnnnn Jun 08 '13 at 06:42
  • Don't know what exactly you want, but the regex is /[a-zA-Z]{4} 0 [0-9]{6}/ and you can use test method to test your input value – Chris Li Jun 08 '13 at 06:49
  • You need to define what letters (“alphabets”) you wish to allow (e.g., is “b” OK? “α”? “þ”?) and whether spaces are allowed and where – the example suggests that they are, but exactly how? The rest is simple, just writing it as a JavaScript regular expression. – Jukka K. Korpela Jun 08 '13 at 14:08

1 Answers1

0

Assuming your validation is client-side:

first four digits are alphabets next digit is zero and next 6 digits are numeric

Compact test:

/[a-zA-Z]{4}\s?0\s?[0-9]{6}\s?$/.test("IBKL 0 001084 ");
/[a-zA-Z]{4}\s?0\s?[0-9]{6}\s?$/.test("IBKL 0 001084");
/[a-zA-Z]{4}\s?0\s?[0-9]{6}\s?$/.test("IBKL0001084");

Verbose test:

var first_four = "[a-zA-Z]{4}",  zero = "0",  next_six = "[0-9]{6}", space_maybe = "\\s?", end = "$";

RegExp(first_four + space_maybe + zero + space_maybe + next_six + space_maybe + end).test("IBKL 0 001084 ");

RegExp(first_four + space_maybe + zero + space_maybe + next_six + space_maybe + end).test("IBKL 0 001084");

RegExp(first_four + space_maybe + zero + space_maybe + next_six + space_maybe + end).test("IBKL0001084");
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265