473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

More than one function per INPUT type ?

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
3 8627
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
4
by: Logico | last post by:
Hi everybody, I need to do a function in javascript to check or uncheck all checkboxes with the same id. I want this function to work in every form and every page of my site, as I will use the same...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
2
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3...
10
by: tadisaus2 | last post by:
Hello, I want to have a user to check at least 2 check boxes and NO more than 2 boxes. I have different checkbox names because I stored each nam on different field. I tried this code but nothing...
1
by: graphicguru | last post by:
Hi i got some javascript from (*) these site for lock one column with js and header row with css. i am not a programmer i am a designer. i am working in a small company. i don't find such...
5
by: mad.scientist.jr | last post by:
According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: I tried writing a reusable function to return the value, see code at:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.