SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onChange check is num ???

    I want to check that the text box does not contain any thing other than numbers when any single key is pressed and do not allow keys other than number.

    How is this possible ???
    chartahir

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    function checkNumber(e)
    {
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    reg = /^[a-zA-Z]+$/;
    return !reg.test(keychar);
    }
    </script>
    
    <body>
    <form>
    <input type="text" onkeypress="return checkNumber(event);" />
    </form>
    Code:
      
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <script type="text/javaScript">
    function valid(f) {
    var re = /^[\d]*$/;
    if (!re.test(f.value)) {
    alert("only numbers are allowed");
    f.value = f.value.replace(/[^\d]/g,"");
    }
    } 
    </script>
    </head>
    <body>
    <input name="" type="text" onkeyup="valid(this)">
    </body>
    </html>
    Code:
    <input onkeyup="this.value = this.value.replace (/\D/, '')">
    Code:
    <script type="text/javascript">
    
    //Only Numbers
    function OnlyNumbers(e)
    {
    	var unicode=e.charCode? e.charCode : e.keyCode
    	if (unicode!=8)
    	{ //if the key isn't the backspace key (which we should allow)
    		if (unicode<48||unicode>57) //if not a number
    			return false //disable key press
    	}
    }
    
    //Only Letters
    function OnlyLetters(e)
    {
    	var key = window.event ? e.keyCode : e.which;
    	var keychar = String.fromCharCode(key);
    	reg = /\d/;
    	return !reg.test(keychar);
    }
    </script>
    
    <input name="Name" type="text" onkeypress="return OnlyLetters(event);">
    
    
    <input name="Name" type="text" onkeypress="return OnlyNumbers(event);">
    Last edited by muazzez; May 5, 2007 at 11:18.
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  3. #3
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the codes, both are good and works fine.

    Thanks Again.
    chartahir

  4. #4
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really useful code.
    Thanx muazzez.


Bookmarks

Posting Permissions

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