js实现:'w5-8' -> /^\w{5,8}$/

<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>

 

posted @ 2015-07-06 11:24  前行者在此  阅读(345)  评论(0编辑  收藏  举报