そうかもしれません。日付形式を指定すると、ユーザーがJavaスクリプト内のテキストボックスのKeyUp/KeyPressイベントを介して入力したものと比較することができます。これは単純ではなく実装することができますjQueryのを使用して
:
各if文でそう
$(function(){
$('#mytextbox').bind('keypress', function(event){
//this assumes that the format is dd-mm-yyyy
//the text in the textbox
var text = $('#mytextbox').val();
//last character entered
var lastCharIndex = (text.length-1) < 0 ? 0 : (text.length -1);
var lastChar = text[lastCharIndex];
if(lastCharIndex == 0){ //corresponds to the first d in dd
} else if (lastCharIndex == 1) { //corresponds to the second d in dd
} else if (lastCharIndex == 2) { //corresponds to the first '-' in dd-mm-yyyy
} else if (lastCharIndex == 3) { //corresponds to the first m
} else if (lastCharIndex == 4) { //corresponds to the second m
} else if (lastCharIndex == 5) { //corresponds to the second '-'
} else if (lastCharIndex == 6) { //corresponds to the first y
} else if (lastCharIndex == 7) { //corresponds to the second y
} else if (lastCharIndex == 8) { //corresponds to the third y
} else if (lastCharIndex == 9) { //corresponds to the forth y
} else { //default error handling
}
});
});
e.keyCode
(または指定したブラウザで相当)の数値以上であるかどうかをチェックする必要があるすべては、「 - 」。私がむしろlastChar
を使用している理由は、これがサポートするはずのブラウザを特定することを迷う必要がないからです。
とにかくlastChar
がテキストボックスのテキストすでに入力されたテキストに1文字しか含まれていない限り、最後に入力された文字はマイナスです。この場合、テキストボックスの内容は空白の''
に設定する必要があります。
ブリーフィター
これは「入力マスキング」と呼ばれます。その言葉を検索するか、ここを見てください:http://www.webresourcesdepot.com/javascript-input-masks/それはあなたの後ろのものかもしれません。 – nickf