如何将4位数字的年份转换为2位数字以进行信用卡到期输入

时间:2019-02-24 17:29:23

标签: jquery html

我有一个输入字段,需要输入年份值,返回2位数字而不是4位数字

示例:从:2019年2月2日起

需要输出,如:02/19

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input id="nmipay-card-expiry" class="input-text" type="text" maxlength="7" autocomplete="off" placeholder="MM / YY" name="nmipay-card-expiry" />

Fiddle here:输入2位数后,在/之后自动添加space/

1 个答案:

答案 0 :(得分:0)

如果您输入了 substr ,则可以使用后2位数字

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

  function GetLast2Digit(){
  
  var year= $("#txtyear").val();
  var LastTwoDigitOfyear = ('' + year).substr(5);
  console.log(LastTwoDigitOfyear); 
  alert("Last 2 digits are "+LastTwoDigitOfyear)
}

</script>
</head>
<body>
Enter year
<input type="text" id="txtyear" placeholder="MM / YY"/>
<button type="button" onclick="GetLast2Digit()">Click</button>
<lable id="lblyear" />
</body>
</html>

相关问题