郵便番号:<input type="text" name="zipcode" >
$('input[name="zipcode"]').on('paste change', function(){
if ( is_postcode( $(this).val() ) ) {
// 正しい郵便番号の時
$(this).after('<p>正しい!</p>');
} else {
// 正しい郵便番号ではない時
$(this).after('<p>間違い!</p>');
}
});
/**
* 7桁のハイフンありなしの郵便番号チェック
* ***-****の形式かどうか判定
* @param {string} postcode 郵便番号
* @return {boolean}
*/
function is_postcode( postcode ) {
if ( postcode.match(/^\d{3}[-]?\d{4}$/) ) {
return true;
} else {
return false;
}
}
This Pen doesn't use any external CSS resources.