如何限制在字母数字文本框中输入9位数字

问题描述:

如何限制用户在字母数字文本区域中输入9位数字. textarea可以有字母.文本区域应允许除9位数字之外的所有数字.

How to restrict user from entering a 9 digit number in alpha numeric textarea. the textarea can have alphabets. The textarea should allow all numbers except 9-digit numbers.

$('#txtName').keyup( function() {
         var str = $(this).val();
         var digits = str.replace(/\D/g, "##");
         var splitdigits = digits.split("##");
       if (splitdigits.length > 0)
       {
         for(var Icount = 0; Icount < splitdigits.length; Icount++)
         { 
          if (splitdigits[Icount] != '' && splitdigits[Icount].match(/^\d+$/) && splitdigits[Icount].length == 9)
            {

                 alert('Invalid input'); 
                 return false;
           }
        }
      }
     });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtName" type="text" >

在脚本标签中使用上面的代码,您可以更改"#txtName"并使用您的textarea ID

Use above code in script tag u can change "#txtName" and use your textarea id