简体   繁体   中英

Input mask for date plus number

Is there way to make date + numbers mask?! I need to make 12/12/15-12345 input or even better 121215-12345. Input mask must check if date is correct and if after "-" is typed only 5 numbers.

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

Please don't blame for code, i'm new in this... Just tryed something.. Would be great if you just make my code work properly if it's possible.

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 !");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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