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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Javascript number check

Status
Not open for further replies.

davejam

Technical User
Jan 6, 2004
313
GB
Hi all,

been a while since i've been dabbling in javascript this much!!!

I have a form, when i leave a field i want to verify its the correct data type ie. number, if not put up a warning then go back to the box with the content highlighted.

This is, as i remembe it, a simple bit of code but for the life of me can i remember it off the top of my head.

can anyone save me some time, i know you have to pas 'this' to the function so you can focus back on the field but i think my brain may explode if i keep trying/searching for the answer.

Cheers



daveJam

*two wrongs don't make a right..... but three lefts do!!!!*
 
There are more ways to do this than there are of skinning a cat... but here's one possible way:

Code:
<input type="text" onblur="checkField(this);" />

...

function checkField(field) {
	if (field.value == '') {
		alert('Field cannot be blank');
		field.focus();
		return(false);
	}

	if (isNaN(field.value)) {
		alert('Field must contain a number');
		field.focus();
		return(false);
	}
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Just looking at it now, got sidetracked with transact side of things, will this work if the fields are decimal(2)?

Anyway, i'll be testing this in just a sec.

Cheers

daveJam

*two wrongs don't make a right..... but three lefts do!!!!*
 
will this work if the fields are decimal(2)?

Probably - because that sounds like aserver-side thing, and should have no bearing whatsoever on the client-side.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top