Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to validate a number when user leaves field

Status
Not open for further replies.

Pr0ject

IS-IT--Management
Jul 8, 2004
7
0
0
US
How can I validate an input text box to make sure it's a number only as they change focus to another field and not via the submit button?
 
<form name=formname>
<input type=text name=number1 onblur=&quot;checkchange(document.formname.number1);&quot;>
</form>

<script language=&quot;javascript&quot;>
function checkchange(oneinput)
{
var digit='0123456789';

for (var oi=0;oi<oneinput.value.length.oi++)
{
var onechar=oneinput.value.substr(oi,1);
if (digit.indexOf(onechar)==-1)
{
alert('You must use only numbers');
oneinput.focus();
oneinput.value='';
return;
}
}
}
</script>
 
one change needed...

for (var oi=0;oi<oneinput.value.length.oi++)

should be

for (var oi=0; oi < oneinput.value.length; oi++)

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top