SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow Numbers only input values in text box? how?

    hi Javascript Gurus.

    My knowledge on Javascript is really very basic.
    i want to have a condition that the input box field should only
    accept numbers only else display error message.

    can you show me guys the solution to this condition.

    Thank you very much in advance.

  2. #2
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <html>
    <head>
    <script type="text/javascript">
    
       function enterNumber(){
    
      var e = document.getElementById('text');
    
      if (!/^[0-9]+$/.test(e.value)) 
    { 
    alert("Please enter onyl number.");
    e.value = e.value.substring(0,e.value.length-1);
    }
    }   
    
    </script>
    </head>
    <body>
    
    Some number: <input type="text" id="text" onkeyup="enterNumber()">
    
    
    
    </body>
    </html>
    I can't add a message.
    Code:
      
    <html>
    <head>
    <script type="text/javascript">
    
    function CheckValue(e){
    e=(window.event) ? event : e;
    
    return (/[0-9]/.test(String.fromCharCode(e.keyCode))); 
    }   
    </script>
    </head>
    <body>
    
    <input type="text" onkeydown="return CheckValue(event)">
    
    </body>
    </html>

  3. #3
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes but if you insert the char in the middle of your text ?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using a server side scripting language like ASP or PHP in your form?

    Controling input is better left to the server than the client, otherwise, if the client has javascript turned off, they won't know that they must only enter numbers.

    Here is the basic idea of you want to do. One thing that you want to account for is the people might want to press enter or backsspace, so you might want to check if the value is not a character, instead of if it IS a number.
    Code:
    <html>
    
    <script type="text/javascript">
    var numString; //this keeps track of the valid string
    
    //check the input after a key press
    function checkinput(e){
    
    //this gets the value of the key pressed
    var aplhaNumericCode=e.keyCode? e.keyCode : e.charCode
    
    //if the key is a number then add it to the number string. (This is not actual js code.)
    if(aplhaNumericCode>=65 //*a*/ || aplhaNumericCode>=90//*z*/)
        putnum()
    else
        alert (keyPressed)
    }
    //enter the valid string in the text box (This is not actual js code.)
    function putnum(){
    var num = document.getElementByID("number")
    num.text = numString 
    }
    </script>
    <body>
    <form>
    <input id ="number" type="text" onkeyup="checkinput(event);" />
    </form>
    </body>


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
  •