Quote:
Originally Posted by esthera
I want only text to be able to be entered and not numbers
but I would like to show a message under the field if someone tries to enter in a number
|
Here you are:-
Code:
<html>
<head>
<style = "text/css">
.message {color:red;font-size:75%; font-weight:bold; }
</style>
</head>
<body>
BOX 1 <input type = "text" id = "txtbox1" onkeyup = "noNums(this,'message1')" onblur = "noNums(this,'message1')" >
<span id = "message1" class = "message"></span>
<br>
BOX 2 <input type = "text" id = "txtbox2" onkeyup = "noNums(this, 'message2')" onblur = "noNums(this,'message2')" >
<span id = "message2" class = "message"></span>
<script type = "text/javascript">
function noNums(which, messbox) {
document.getElementById(messbox).innerHTML = "";
var val = which.value;
var len = val.length;
if (/\d/.test(val)) {
val = val.substring(0,len-1);
which.value = val;
document.getElementById(messbox).innerHTML = "You may not enter numbers in this box!";
}
}
</script>
</body>
</html>
Only numbers are prohibited - all other characters are allowed. So someone could type "two".
It is probably better to display the message alongside the input box as I have done rather than below it, but you can easily adjust that.
“A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)