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.

  
<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.

  
<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>

yes but if you insert the char in the middle of your text ?

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.


<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>