[thelist] js form validation problem - still not working

Burhan Khalid burhankhalid at members.evolt.org
Mon Jun 17 03:38:01 CDT 2002


>
> That is interesting.  I like your thinking, and it works - except that
> it doesn't validate the 2nd input.  I can't call the function
> again from
> within itself, can I..?  (scared to... will it crash the computer??!)
>

How do you mean within itself. Are you talking about recursion?
Also, I didn't get why you had two functions that did the same thing (in
your nl.js).
So, here is something that I did :

function isNumeric(number) {
	if (!parseFloat(number)) { return false; }
		else { //the first digit was numeric, so check the rest
				for (var i=0; i<number.length; i++) {
					if ((number.charAt(i) != "0")
					&&
(!parseFloat(number.charAt(i)))) { return false;	}
				}
			 }
	return true;
}


function chkStockNum(fld, num, wine) {

	var sv = fld.value
	if (!isNumeric(sv)) {

		fld.value = prompt("Please enter a valid number for the
bottles of "+wine);
		return false;
	}
	else if (sv > num) {
		fld.value = prompt("There are only " + num + " bottles
of " + wine + " available.  Please enter a lower number.");
		return false;
	}
	return true;

> Still also curious about why the first doesn't work.  It just doesn't
> make sense.

I think its because once you hit the alert button, then it shifts focus
to the next one, because it assumes
that you are done with that field. I tried this

if(alert()) { setfocus(); } but no avail.

I've seen similar behavior in different programming languages. (I'm not
a JS expert), so I just chalked it up
to that.

hth
Burhan





More information about the thelist mailing list