Click to See Complete Forum and Search --> : Help needed


Alv
05-07-2003, 02:26 AM
I have a numerical checking function.

function IsNumeric(field) {
var valid = "0123456789"
var ok = "true";
var temp;
for (var i = 0; i < field.value.length; i++) {
temp = "" + field.value.substring(i, i + 1);
if (valid.indexOf(temp) == "-1")
ok = "false";
}
if (ok == "false") {
alert("Invalid input. Please enter only numbers.");
field.focus();
field.select();
}
}

I have this HTML input textbox.

<INPUT TYPE="text" NAME="Quantity" MAXLENGTH="3" SIZE="3" VALUE="0" CLASS="text" onChange="IsNumeric(this); document.Form.Manpower.value = document.Form.Quantity.value * document.Form.Day.value; document.Form.Amount.value = document.Form.Manpower.value * document.Form.Rate.value">

My question, how do I prevent the subsequent javascript from executing if the user input non numeric data? I do not want this portion to be executed "document.Form.Manpower.value = document.Form.Quantity.value * document.Form.Day.value; document.Form.Amount.value = document.Form.Manpower.value * document.Form.Rate.value".

Thanks in advance.

Charles
05-07-2003, 05:31 AM
Just test for it before you run that other function. By the way, JavaScript has several built in ways to tell if something is a number. Given n might be some number:

if (Number(n)) {}

if (/-?\d*(\.\d+)/.test(n)) {}

Alv
05-07-2003, 08:33 PM
Sorry but the result is the same as my original. The portion below has to be inserted into every entry since the number of repetition depends on how many records are there in the database. I can't put them into a function because I won't know their referencing by then. The referencing goes something like this document.Form.Day1.value, document.Form.Day2.value, document.Form.Day3.value... etc.

"document.Form.Manpower.value = document.Form.Quantity.value * document.Form.Day.value; document.Form.Amount.value = document.Form.Manpower.value * document.Form.Rate.value"

I am looking at something which stop it at its track if a non numeric is inputted, I have tried using "return" but to no avail.

e.g onChange="return IsNumeric();..."

Using "return" does stop processing on its track but it won't also continue processing even a numeric is passed in. You can see the page by following the URL below.

http://www15.brinkster.com/gyp/numeric.htm

Thanks in advance.

Charles
05-07-2003, 08:58 PM
onchange="if (Number(this.value)) {document.Form.Manpower.value = document.Form.Quantity.value * document.Form.Day.value; document.Form.Amount.value = document.Form.Manpower.value * document.Form.Rate.value} else {alert('Invalid input. Please enter only numbers.'); this.value = ''; this.focus}"

Jona
05-07-2003, 09:35 PM
Charles, would that be this.focus() instead of this.focus? Since focus is a method not a property (am I right?).

Charles
05-08-2003, 04:47 AM
Yes, it would. Damn those typeo's

Jona
05-08-2003, 09:55 AM
Yeah.... :p