验证数字和字母表的特定组合的表单字段

时间:2013-06-08 06:40:33

标签: regex validation sample alphabet

我需要验证特定类型的数字和字母组合的表单字段:

  • 前四位是字母
  • 下一位数为零
  • 接下来的6位数字是数字

e.g。
IBKL 0 001084

1 个答案:

答案 0 :(得分:0)

假设您的验证是客户端的:

  

前四位是字母,下一位是零,接下来的6位是数字

紧凑测试:

/[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");

详细测试:

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");