<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Form Example</title>
<form action="foo.pl">
<p>
<label for="number">Number:</label>
<br>
<input id="number" type="text" onchange="if (/[^\d\.]/.test(this.number.value) {alert('This field requires a Number'); this.value=''; this.focus()}">
</p>
<p>
<input type="submit">
</p>
</form>
But like all JavaScript, this will fail for a great number of users. In this case about 12% of them.
And if you want something that tests more specifically for a number (id est only one decimal point, negative numbers permitted) then this might do the trick:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Form Example</title>
<form action="foo.pl">
<p>
<label for="number">Number:</label>
<br>
<input id="number" type="text" onchange="if (/^\.?$/.test(this.value) || !/^-?\d*\.?\d*$/.test(this.value)) {alert('This field requires a Number'); this.value=''; this.focus()}">
</p>
<p>
<input type="submit">
</p>
</form>
Last edited by Charles; 01-31-2003 at 06:26 AM.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Bookmarks