By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

More than one function per INPUT type ?

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Craig wrote:

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

Jul 20 '05 #2

P: n/a
Guido Wesdorp wrote on 02 Dec 2003:
Craig wrote:

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*...


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

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #3

P: n/a
JRS: In article <Xn*******************************@193.38.113.46 >, seen
in news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.uk.
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.