More than one function per INPUT type ?

C

Craig

I currently have the following code:

<input name="castStation" type="text" value="0"
onChange="valueCalculate()";>

The function called does a few caluclations and writes the total to a
total box.

However, if the user type a non-numeric char, I get NaN.

Is there a way to only allow numeric entry? I tried to add a
onKeyPress, but the problem is I don't think you can have more than
one function per <input..>, or can you?
 
G

Guido Wesdorp

Craig said:
Is there a way to only allow numeric entry? I tried to add a
onKeyPress, but the problem is I don't think you can have more than
one function per <input..>, or can you?

Sure you can... Just add the event handler to the HTML and it should
work. I think using JavaScript you can actually register more than one
function *per event type*...

Cheers,

Guido
 
M

Michael Winter

Guido Wesdorp wrote on 02 Dec 2003:
Sure you can... Just add the event handler to the HTML and it
should work. I think using JavaScript you can actually register
more than one function *per event type*...

Is this what you mean?

<INPUT ... onclick="myFirstFunction();mySecondFunction();[etc...]">

In that example, myFirstFunction(), followed by mySecondFunction(),
followed by...(until the end of the list) would be executed on each
click.

To Craig - a slight syntax error in your example:

<input name="castStation" type="text" value="0"
onChange="valueCalculate()";>
^
That semi-colon shouldn't be there. It doesn't need to be present in
the intrinsic event, either - only if there is more than one
statement.

The best way to cover this really is to just test for NaN and alert
the user. There are too many avenues to cover that can interfere with
other functionality when restricting entry. Instead, do something
like this:

// Returns true if is valid number, false otherwise
//
function isValidNumber( num ) {
return !isNaN( Number( num )));
}

If you only want to validate integers, for example, you could use:

function isValidInt( num ) {
return !isNaN( parseInt( num )));
}

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in Michael Winter <[email protected].
invalid> posted at Tue, 2 Dec 2003 16:21:13 :-
If you only want to validate integers, for example, you could use:

function isValidInt( num ) {
return !isNaN( parseInt( num )));
}

One ) too many. Moreover, it accepts such as 0x77 and 3+3.

Better to use a RegExp, especially if limiting the number of digits is
good.

Your num is in fact a string, and could be so named.

function isValidInt(Str) { return /^\d{1,12}$/.test(Str) }

for non-negative integers of 1..12 decimal digits.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm> and its links.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top