Results 1 to 4 of 4

Thread: Allow only numbers !!!

  1. #1

    Thread Starter
    Lively Member bhaskerg's Avatar
    Join Date
    Mar 2001
    Location
    india
    Posts
    68

    Cool

    In javascript how to check for a textbox to allow only numbers to be typed and not to allow characters and special characters like ' " etc...

  2. #2
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    all single digit (pos, real) numbers are either greater than or equal to zero, or less than or equal to 9. So just run through the string and look for something that doesn't fit this description.

    Code:
    for (i=0; i<passedValue.length; i++) {
    if (passedVal.charAt(i) < "0") { return false}
    if (passedVal.charAt(i) > "9") { return false}
    }

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    or like this:

    Code:
     
    <HTML> 
    <HEAD>
    <script Language="JavaScript">
    function check(contents) 
    {
        	if (((contents / contents) != 1) && (contents != 0)) 
    	{
    		alert('Please enter only numbers into this text box')
    	}
    }
    
    </script>
    </HEAD> 
    <BODY>
    
    <input name="txt1" onBlur="check(this.value)">
    </BODY>
    </HTML>
    Mark
    -------------------

  4. #4
    John Slade
    Guest
    Or you could be really flash and use regular expressions to do it:-

    Code:
    <HTML> 
    <HEAD>
    <script Language="JavaScript">
    function check(contents) 
    {
    	var pattern = /[0-9]/;
    
    	flag = pattern.test(contents);
    
        if (!flag) {
    	
    		alert('Please enter only numbers into this text box')
    		
    	}
    }
    
    </script>
    </HEAD> 
    <BODY>
    
    <input name="txt1" onBlur="check(this.value)">
    </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
  •  



Click Here to Expand Forum to Full Width