<input type="text" id ="box1" data-type="w5-8"> <span id="txt1"></span>
方法1: <script> $(function () { var iptbox = $('#box1'), data1 = $('#box1').data('type'), zimu = data1.match(/^[A-Za-z]+/), num = data1.match(/\d+/g), testbox =new RegExp('^\\'+zimu[0]+'{'+num+'}$'); iptbox.blur(function(){ $('#txt1').text(function(){ return testbox.test(iptbox.val()) ? '' : '请输入正确信息'; }); }); }) </script>
方法2: <script> $(function () { var iptbox = $('#box1'), data1 = $('#box1').data('type'); var zzStr = data1.replace(/w(\d+)-(\d+)$/, '^\\w{$1,$2}$'); testbox = new RegExp(zzStr); iptbox.blur(function(){ $('#txt1').text(function(){ return testbox.test(iptbox.val()) ? '' : '请输入正确信息'; }); }); }) </script>