简体   繁体   English

正则表达式不适用于验证信用卡到期信息

[英]regex expression not working for validating credit card expiry information

I have a regex expression which matches the expiry date on credit card Format I am looking for is mm/yy. 我有一个正则表达式,它与我要查找的信用卡格式上的到期日期匹配,是mm / yy。 Following is my function in javascript 以下是我在javascript中的功能

validateCreditCardExpiry: function(expiry){
        if(this.isEmpty(expiry))
        {
            return false;
        }
        var regex = /^(0[1-9]|1[0-2])\/?([0-9]{2})$/;
        if(!regex.test(expiry))
        {
            return false
        }
        return true
    }

I have tried using the above on string such as 12/33 which returns false. 我试过在返回false的字符串(例如12/33)上使用以上代码。 Can some one help me out. 有人可以帮我吗。 Thanks 谢谢

In addition to my comment ( see your working regex here ), you can very well shorten your function: 除了我的评论( 请参阅此处的正则表达式 )之外,您还可以很好地缩短功能:

validateCreditCardExpiry: function(expiry){
    if (expiry) {
        var regex = /^(0[1-9]|1[0-2])\/?([0-9]{2})$/;
        if(regex.test(expiry)) return true;
    }
    return false;
}

Or - as @anubhava pointed out in the comments, even shorter: 或者-如@anubhava在评论中指出的,甚至更短:

validateCreditCardExpiry: function(expiry){
    return /^(0[1-9]|1[0-2])\/?[0-9]{2}$/.test(expiry);
}

The above regex expression is working correctly. 上面的正则表达式可以正常工作。 I somehow was introducing spaces between '/'. 我不知何故在'/'之间引入了空格。 that was the problem 那是问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM