0

日付+数字マスクを作成する方法はありますか?! 12/12/15-12345 を入力するか、121215-12345 を入力する必要があります。定型入力は、日付が正しいかどうか、および「-」の後に 5 つの数字のみが入力されているかどうかを確認する必要があります。

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

コードのせいにしないでください、私はこれが初めてです...何かを試してみました..可能であれば、私のコードが適切に機能するようにしていただければ幸いです。

4

1 に答える 1

0
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 !");
}
于 2015-05-19T11:09:11.920 に答える