繁体   English   中英

输入日期和数字的掩码

[英]Input mask for date plus number

有没有办法制作日期和数字掩码? 我需要输入12/12 / 15-12345甚至更好的121215-12345。 输入掩码必须检查日期是否正确以及在“-”之后仅键入5个数字。

> var dateMask = new InputMask(JST_MASK_DATE+[fieldBuilder.literal("-"), fieldBuilder.inputNumbers(1, 5)], "date");

请不要为代码而怪,我是新来的...尝试一下..如果可以的话,只要让我的代码正常工作,那就太好了。

var date = '121215-13456'; // Here is the string you begin with
var valid = true; // if valid === true, the string is correct

// This will check the structure of the string and store the values in result
var result = /^(\d{2})(\d{2})(\d{2})-\d{5}$/.exec(date);

// If i didn't match the mask
if (result === null)
    valid = false;
else
{
    var myDate = new Date(2000+parseInt(result[3]), (parseInt(result[2]) - 1), parseInt(result[1]));
    // If the date is incorrect
    if ((myDate.getMonth() + 1 != parseInt(result[2])) || (myDate.getDate() != parseInt(result[1])) || (myDate.getFullYear() != 2000 + parseInt(result[3])))
        valid = false;  
}

if (!valid)
{
    alert("Invalid");
}
else
{
    alert("Valid !");
}

暂无
暂无

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

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