Regular expressions can be handy for a lot of things, but for simpler tasks they are not the most efficient way. I would suggest determining if the field is not a number and is of a certain length:-
Code:
<html>
<head>
<script type="text/javascript">
function checkNumber(id,num) {
// num is a number already so no need for parseInt(), which in any case ought to specify the radix
var elementTxt = Number(document.getElementById(id).value);
if ((isNaN(elementTxt)) || (elementTxt.toString().length != num)) {
alert ("Please enter a valid " + num + "-digit number inside form");
document.getElementById(id).value = ""; // clear the field
return false;
}
}
</script>
</head>
<body>
<input type="text" id='txt' size = "5" maxlength = "5"/>
<input type="button" value="Check It" onclick="checkNumber('txt',5)"/>
</body>
</html>
The value of a form field can never be null. Only "" (blank).
You ought to know that when posting here you are asked to help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
Protestors Tried To Spoil Play But Actors Succeeded - Headline in Surrey Adveriser