使用JavaScript

时间:2017-04-27 03:29:29

标签: javascript date

我正在为学校工作,我们正在使用JavaScript(我只允许使用JavaScript)来验证付款页面的表单。这是我第一次使用JavaScript而且感觉有点失落......

我面临着验证到期日期的任务,并且要求是:

  1. 数字必须正好为2-2位(格式为mm-yy)
  2. mm需要> 01且<12
  3. 且有效期必须在今天的日期之后
  4. 到目前为止,我只能指定前两个要求,而且我无法确定如何根据今天的日期查看日期。

    HTML

    <form>
    <label for="expiryDate">Expiry Date</label>
    <input type="text" name="expiryDate" id="expiryDate" />
    
    <input type="submit" value="Check out" />
    </form>
    

    的JavaScript

    var expiryDate = document.getElementById("expiryDate").value;
    
    if (!expiryDate.match(/^[0][1-9]|[1][0-2]-[0-9]{2}$/)){
        errMsg = errMsg + "The expiry date needs to be mm-yy and consist of a valid date.\n";
        result = false;
    } 
    

    所以,如果有人知道如何帮助我,我将不胜感激!

2 个答案:

答案 0 :(得分:0)

这样的事情应该有所帮助

var today = new Date(); // gets the current date
var today_mm = today.getMonth() + 1; // extracts the month portion
var today_yy = today.getFullYear() % 100; // extracts the year portion and changes it from yyyy to yy format

if(today_mm < 10) { // if today's month is less than 10
    today_mm = '0' + today_mm // prefix it with a '0' to make it 2 digits
} 

var mm = expiryDate.substring(0, 2); // get the mm portion of the expiryDate (first two characters)
var yy = expiryDate.substring(3); // get the yy portion of the expiryDate (from index 3 to end)

if (yy > today_yy || (yy == today_yy && mm >= today_mm)) {
   // all good because the yy from expiryDate is greater than the current yy
   // or if the yy from expiryDate is the same as the current yy but the mm
   // from expiryDate is greater than the current mm
}
else
{
   errMsg = errMsg + "The expiry date needs to be greater than today.\n";
   result = false;
}

答案 1 :(得分:0)

我总是对程序员宁愿花费精力为用户构建直接夹克而不是编写用户友好代码感到惊讶。

月份标准似乎不正确,“&gt; 01和&lt; 12”推断出2到11之间的值,我希望“&gt; = 1且&lt; = 12”。

要完成分配所需的操作,您可以执行以下操作:

/* Check that the provided string is:
**  - exactly 2-2 digits in the format mm-yy
**  - mm is >= 01 and <= 12
**  - expiry date is this month or later
*/
function validateExpiryDate(s) {

  // Check 2-2 digits
  if (!/\d\d-\d\d/.test(s)) {
    return 'Expiry date format must be MM-YY: ' + s;
  }
  
  // Check month is 1 to 12 inclusive
  var b = s.split('-');
  if (b[0]<1 || b[0]>12) {
    return 'Expiry month must be from 00 to 12: ' + b[0];
  }
  
  // Check is this month or later
  var d = new Date()
  var c = d.getFullYear()/100 | 0 + '';
  if (new Date(c + b[1], b[0], 1) < d) {
    return 'Expiry date must be this month or later: ' + s;
  }
  
  return true;
}

// Some tests
['01-17','12-17','foo','13-20','9-18'].forEach(function(s){
  console.log(validateExpiryDate(s));
})

但是,为了更加用户友好,您可以非常轻松地接受单个数字和各种分隔符。此外,您可以切换到追逐并测试输入的值是否生成合适的到期日期。您还可以将输入的字符串重新格式化为所需的格式,例如:

// Validate expiry date in m-yy format
// Separator can be any non-digit
function checkExpiryDate(s) {
  var b = s.split(/\D/);
  var d = new Date();
  var century = d.getFullYear()/100 | 0;
  // Generate date for first day of following month
  var expires = new Date(century + b[1], b[0], 1);
  return d < expires;
}

// Reformat date in m/yy format to mm-yy strict
function formatExpiry(s) {
  var b = s.split(/\D/);
  function z(n){return (n<10?'0':'') + +n}
  return z(b[0]) + '-' + z(b[1]);
}

['1/1','4-17','03-17','3-18','06/19','3.19','foo'].forEach(function(s) {
  console.log('String "' + s + '" converts to "' + 
               formatExpiry(s) + '" and is ' + 
               (checkExpiryDate(s)? '' : 'not ') + 'valid.');
});

因此,用户友好也是编写的代码较少。 ; - )

计算这个世纪有点矫枉过正,你可以使用“20”并期望你的代码在2095年没有运行,届时到期日将开始有几个世纪的“21”。