Friday, 28 September 2012

How to make Numeric,String textbox and many more on a webpage using java script

Here i'm showing code which help you to make
a textbox which can only accept number.
This is helpful when you're making texbox which takes number as input
This solution works in both IE and Mozilla.
Numeric  Textbox..
HTML Code :
<html>
<head >
    <title>Girfa Numeric Textbox Demo</title>
   
    <script language="javascript">
        function validatenumber(evt)
        {
           
          var ch = (evt.which) ? evt.which : event.keyCode
          if(ch > 31 && (ch < 48 || ch > 57))         
            return false;
                   
          return true;                     
            
        }
    </script>
</head>
<body >

        <input type="text" onkeypress="return validatenumber(event)" id="t1" name="text" />              

</body>
</html>
Text Box Which takes Only String  ...
<html>
<head >
    <title>Girfa String  Textbox Demo</title>
  
    <script language="javascript">
        function textTextBox(evt) {
       evt = (evt) ? evt : event;
       var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
          ((evt.which) ? evt.which : 0));
       if (charCode > 31 && (charCode < 65 || charCode > 90) &&
          (charCode < 97 || charCode > 122)) {
          alert("Enter letters only.");
          return false;
       }
       return true;
     }
       
    </script>
</head>
<body >
    <input type=text onkeypress="return textTextBox(event)" />
</body>
</html>

1 comment:

  1. i was searching for this type of code for my webpage i have done a little changes to make it for text only which i am sharing with you:-
    make this changes in function
    if(ch>=65 && ch<=90 || (ch>=97 && ch<=122))
    return true;

    return false;
    please give your feedback for this code
    thanks

    ReplyDelete