首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JavaScript中验证信用卡到期日期与今天的日期

在JavaScript中验证信用卡到期日期与今天的日期
EN

Stack Overflow用户
提问于 2017-04-27 03:29:29
回答 2查看 7K关注 0票数 1

我正在为学校做一项作业,在那里我们使用JavaScript (我只允许使用JavaScript)来验证支付页面的表单。这是我第一次和JavaScript合作,所以我觉得有点迷茫.

我面临着验证到期日期的任务,要求如下:

  1. 这个数字必须是2-2位数字(以mm-yy格式)。
  2. 毫米需要大于01和<12
  3. 截止日期必须在今天之后

到目前为止,我只能够指定前两个要求,并且我很难弄清楚如何对照今天的日期检查日期。

html

代码语言:javascript
复制
<form>
<label for="expiryDate">Expiry Date</label>
<input type="text" name="expiryDate" id="expiryDate" />

<input type="submit" value="Check out" />
</form>

JavaScript

代码语言: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;
} 

所以,如果有人知道如何帮助我,我会很感激的!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-27 03:45:32

像这样的东西会有帮助的

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2017-04-27 23:02:18

我总是感到惊讶的是,程序员宁愿把他们的精力花在为用户做直夹克上,而不是编写用户友好的代码。

月份标准似乎是不正确的,">01和<12“推断了一个从2到11的值,我希望">=1和<=12”是有意的。

要准确地完成任务所需的内容,您可以执行以下操作:

代码语言:javascript
复制
/* 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));
})

但是,为了使用户更加友好,您可以很容易地接受个位数和各种分隔符。此外,您也可以直接切入追逐,并测试输入的值是否生成一个合适的终止日期。您还可以将输入的字符串重新格式化为所需的格式,例如:

代码语言:javascript
复制
// 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”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43648170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档