1

我正在做一个项目,根据给定的 HallTicket Number 显示学期分数。

因此,我必须使用 JavaScript 对输入的大厅票号进行验证。

并且大厅票号有以下限制:

--->It shouid hav exactly 10 characters
--->starting two positions may be 10 or 11 or 12
--->next four positions must be the string "kp1a"
--->later two positions may contains any of 02 or 04 or 05 or 12 only
--->remaing two positions may any digits from 01 to 60.

谁能说如何编写 JavaScript 来验证这种大厅票号?

4

1 回答 1

2

一个(稍微测试过的)非正则表达式解决方案

function validateTicketNum(numb){
   var first = ["10","11","12"];
   var third = ["02","04","05","12"];
   if (numb.length!==10){
      return false;
   }
   else if (first.indexOf(numb.substring(0,2))==-1){
      return false;
   }
   else if (numb.substring(2,6)!=='kp1a'){
      return false;
   }
   else if (third.indexOf(numb.substring(6,8))==-1){
      return false;
   }
   else if (isNaN(numb.substring(8,10)) || 
            parseInt(numb.substring(8,10),10) < 1 ||
            parseInt(numb.substring(8,10),10) > 60){
      return false;
   }
   return true;
}
于 2013-01-22T11:42:33.557 回答