www.webdeveloper.com
Results 1 to 9 of 9

Thread: textbox field problem

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    textbox field problem

    I need to code one of my field to accept only numbers and the maximum number should be 3. Howto achieve that? I'm still learning.

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function zero_three_only(txt) {
    // Allows only numbers 0-3 to be entered into a text or textarea
    	var allowOnly = /^[0123]{1,}$/g;
    	if (txt.value.match(allowOnly) === null) {
    		txt.value = txt.value.substr(0,(txt.value.length-1));
    		}
    	}
        
       
    </script>
    </head>
    
    <body>
    <form>
    <textarea name="thisText" onKeyUp="return zero_three_only(this);"></textarea>
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Nov 2010
    Posts
    978
    Code:
    <input type='text' onkeydown="oldval=this.value" onkeyup="if(isNaN(this.value)||this.value[this.value.length-1]>3){this.value=oldval}" />
    Last edited by xelawho; 04-08-2012 at 02:04 PM.

  4. #4
    Join Date
    Apr 2012
    Posts
    4
    Its not working as it should. Normally it is a textbox to input grade 0-100 and the number should be in that range. How to do that?

  5. #5
    Join Date
    Nov 2010
    Posts
    978
    Code:
    <input type='text' onkeyup="if(isNaN(this.value)||this.value>100){this.value=''}" />

  6. #6
    Join Date
    Apr 2012
    Posts
    4
    xelawho
    its doesn't work. it still accepts 101 which infact should have been only between 0-100

  7. #7
    Join Date
    Nov 2010
    Posts
    978
    Quote Originally Posted by Yusuf_01 View Post
    it still accepts 101 which infact should have been only between 0-100
    tested in IE, FF, Chrome and Safari - any number entered over 100 gets deleted.

    either you're using some funky browser (not so likely), your implementation is bad (quite likely) or you have a conflict with some other script that is running (who knows?)

    maybe you should show the code you're using?

  8. #8
    Join Date
    Apr 2012
    Posts
    4
    Yeah any numbers after 100 is deleted automatically with the code only. I tested just now. But when i put it on my form, it accepts any numbers with three char

  9. #9
    Join Date
    Nov 2010
    Posts
    978
    Quote Originally Posted by xelawho View Post
    maybe you should show the code you're using?

Thread Information

Users Browsing this Thread

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

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