You can make a Regular Expression for that of course, and that is very simple, but in your particular case, very simple one, it's much better to filter out unwanted keystrokes while the user types the text.
This is a simple code sample:
Here is how to filter out:
<html>
<head>
<script type="text/javascript"><!--
function filterDigits(eventInstance) {
eventInstance = eventInstance || window.event;
key = eventInstance.keyCode || eventInstance.which;
if ((47 < key) && (key < 58) || key = 45 || key == 8) {
return true;
} else {
if (eventInstance.preventDefault) eventInstance.preventDefault();
eventInstance.returnValue = false;
return false;
}
}
--></script>
</head>
<body">
<input type="text" onkeypress="filterDigits(event)"/>
</body>
</html>
Pay attention for the character #8. This is a backspace, which you should allow for input specifically. You can modify this code to allow characters of your choice.
—SA