此处错误是无法输入任何字符:
$(document).ready(function() {
$('#t').keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt + ' : ' + e.which);
if(!txt.match(/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/)){
return false;
}
})
});
答案 0 :(得分:0)
pattern
属性广泛。因此,您只需将其添加到<input>
<input pattern="^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$" />
答案 1 :(得分:0)
你可以这样做:
<input type="text" id="postCode" placeholder="Enter postcode">
<span id="result"></span>
var pattern = /[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]/,
$result = $("#result");
$('#postCode').keyup(function(){
var val = this.value
if(!val.match(pattern)){
$result.text("invalid");
} else {
$result.text("valid");
}
});
这里是demo