Advertisement
johnburn

Sample Code for [email protected]

May 19th, 2011
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.32 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-latest.js"></script>
  5. <script>
  6. $(document).ready(function () {
  7.     // Filter input box lenght
  8.     var maxLength = 15;
  9.     $('#counter').html(maxLength + " characters left");
  10.     $('#input1').keyup(function (e) {
  11.         var currentlen = $('#input1').val().length;
  12.         if (currentlen <= maxLength) {
  13.            $('#counter').html(maxLength - currentlen + " characters left");
  14.        } else {
  15.            $(this).val($(this).val().substring(0, maxLength));
  16.        }
  17.    });
  18.    //Filter number sahaja
  19.    $("#input2").keydown(function (e) {
  20.        if (e.shiftKey) {
  21.            e.preventDefault();
  22.        }
  23.        if (e.keyCode == 46 || e.keyCode == 8) {} else {
  24.            if (e.keyCode < 95) {
  25.                if (e.keyCode < 48 || e.keyCode > 57) {
  26.                     e.preventDefault();
  27.                 }
  28.             } else {
  29.                 if (e.keyCode < 96 || e.keyCode > 105) {
  30.                     e.preventDefault();
  31.                 }
  32.             }
  33.         }
  34.     });
  35. });
  36. </script>
  37. </head>
  38. <body>
  39. <fieldset>
  40.     <label><span>Input 1: </span></label><input type="text" id="input1"/><span id="counter"></span><br/><br/>
  41.     <label><span>Input 2: </span></label><input type="text" id="input2"/>
  42. </fieldset>
  43. </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement