www.webdeveloper.com
Results 1 to 3 of 3

Thread: Prevent a user from entering text in a text area

  1. #1
    Join Date
    May 2010
    Posts
    36

    Prevent a user from entering text in a text area

    Hello, I have a text area, I want that user's should not be able to enter a charaters in it. I want them to enter only numbers.
    I have written the code below to do this but it is not working as I think I am not calling the function right way: can anyone rectify my code:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function CheckNumeric()
    {
       // Get ASCII value of key that user pressed
       var key = window.event.keyCode;
    
       // Was key that was pressed a numeric character (0-9)?
       if ( key > 47 && key < 58 )
          return; // if so, do nothing
       else
          window.event.returnValue = null; // otherwise, 
    	                               // discard character
    }
    </script>
    </head>
    <body>
    <input type="text" name="text" value=""></input>
    <input type="button" name="button1" value="Check" onclick="return CheckNumeric();"></input>
    </body>
    </html>
    I am open to new suggestions as well, if I can show some alert to user if they enter character rather than no.

    your help is welcome

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    I assume you are trying to do this ...
    see: http://www.htmlcodetutorial.com/form...msupp_158.html
    It has a working example included.

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    This is one way of doing it:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title></title>
    <
    script type="text/javascript">
    <!--
     
    function 
    chkIfNumeric(elem) {
         
    str elem.value;
        
    //check if the last char in str is a number
        
    if(isNaN(str.charAt(str.length-1)) || str.charAt(str.length-1) == ' ') {
         
    str str.substring(0,str.length-1);   //remove the last character
         //write str back to the text box
         
    elem.value str;
        }
    }
     
    //-->
    </script>

    </head>
    <body>
     
    <div>
         <input type="text" id="txtMyText" onkeyup="chkIfNumeric(this)" />
    </div>
     
    </body>
    </html> 
    Last edited by tirna; 06-16-2010 at 12:45 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles