Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    restricting the user from entering the number >150

    hi frnds

    i want to restrict the user from entering the number greater than 150 in the textbox.

    like if the user enters 1-9 in the first position its fine..., in the same way in the second position also. ex: 99..now i shoudnt allow him to type anyohter number coz it wil exceede 150.

    i hope the question was clear..

    thnx
    Murali

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this a try

    PHP Code:
    <script type="text/javascript">

    function 
    chk150(){

    re = /^[0-9]*$/
    if (!
    re.test(document.f1.t1.value)){
    document.f1.t1.value document.f1.t1.value.replace(/[^0-9]/g,"")
    }

    if(
    document.f1.t1.value>150){
    document.f1.t1.value=""
    }

    }

    </script>

    <form name="f1">
    <input type="text" name="t1" onkeyup="chk150()">
    </form> 
    Last edited by Mr J; 01-06-2007 at 06:31 PM.

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could try something like this. I think I've seen solutions that don't require you to see the incorrect value but I can't think what they are right now.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Junque</title>
        <script type="text/javascript">
        /* <![CDATA[ */
          var oldValue = "";
          function m(oThis) {
            var s = oThis.value;
            var n = parseInt(s);
    
            if (n > 150) {
              oThis.value = oldValue;
            } else if (/[^0-9]/.test(s)) {
              oThis.value = oldValue;
            }
            oldValue = oThis.value;
          }
        /* ]]> */
        </script>
      </head>
      <body>
        <div>
          <form name="f" action="#" onsubmit="return(false);">
            <input type="text" name="t" onkeypress="m(this);" onkeydown="m(this);" onkeyup="m(this);" />
          </form>
          <br />
          <div id="adiv"> </div>
        </div>
      </body>
    </html>
    david_kw

  • #4
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    murali1983:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    	var max = 150;
    
    	function check(nField){
    		
    		if (!/^\d+$/.test(nField.value)){nField.value = nField.value.substring(0,nField.value.length-1)}
    		if (Number(nField.value) > max){alert('Maximum is '+max);nField.value = ""}
    	}
    
    </script>
    </head>
    <body>
    
    	<form>
    		<label>A: <input type='text' size='5' name='a' onkeyup="check(this)"></label><br>
    		<label>D: <input type='text' size='5' name='d' onkeyup="check(this)"></label>
    	</form>
    
    </body>
    </html>


  •  

    Posting Permissions

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