Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

More than one function per INPUT type ?

Question posted by: Craig (Guest) on July 20th, 2005 12:29 PM
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?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Guido Wesdorp's Avatar
Guido Wesdorp
Guest
n/a Posts
July 20th, 2005
12:29 PM
#2

Re: More than one function per INPUT type ?
Craig wrote:[color=blue]
>
> 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?[/color]

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


Michael Winter's Avatar
Michael Winter
Guest
n/a Posts
July 20th, 2005
12:29 PM
#3

Re: More than one function per INPUT type ?
Guido Wesdorp wrote on 02 Dec 2003:
[color=blue]
> Craig wrote:[color=green]
>>
>> 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?[/color]
>
> 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*...[/color]

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
Join Bytes!lid (remove ".invalid" to reply)

Dr John Stockton's Avatar
Dr John Stockton
Guest
n/a Posts
July 20th, 2005
12:30 PM
#4

Re: More than one function per INPUT type ?
JRS: In article <Xns9445A65BA3C30MWinterBlueyonder@193.38.113.46>, seen
in news:comp.lang.javascript, Michael Winter <M.Winter@blueyonder.co.uk.
invalid> posted at Tue, 2 Dec 2003 16:21:13 :-[color=blue]
>
>If you only want to validate integers, for example, you could use:
>
>function isValidInt( num ) {
> return !isNaN( parseInt( num )));
>}[/color]

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.

 
Not the answer you were looking for? Post your question . . .
183,814 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors