473,406 Members | 2,698 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,406 software developers and data experts.

textboxes addition

I have a very simple webpage (html form). There are about 50 input
textboxes and users are expected to insert data. No I would like to
know how in javascript I can check the values in all textboxes not to
exceed 1000 in total. I want to click the button that submits the data
on the server, to fire a javascript check the values and if less than
1000 then send them over otherwrise popup a message window and say
values exceed 1000.

Thanks a lot
Marios Koumides

Jul 23 '05 #1
4 1789
<ko******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I have a very simple webpage (html form). There are about 50 input
textboxes and users are expected to insert data. No I would like to
know how in javascript I can check the values in all textboxes not to
exceed 1000 in total. I want to click the button that submits the data
on the server, to fire a javascript check the values and if less than
1000 then send them over otherwrise popup a message window and say
values exceed 1000.

Thanks a lot
Marios Koumides


Will this help? Watch for word-wrap.

<html>
<head>
<title>max1000.htm</title>
<script type="text/javascript">
function bytes() {
var max = 0;
for (var i=0; i<document.forms[0].elements.length; i++) {
if (document.forms[0].elements[i].type == "text") {
max += document.forms[0].elements[i].value.length;
}
}
window.status = max;
if (max <= 1000) return true;
alert("1,000 character maximum exceeded!");
return false;
}
</script>
</head>
<body>
<form onsubmit="return bytes()">
<input type="text" name="t001" value="" onblur="bytes()">
<input type="text" name="t002" value="" onblur="bytes()">
</form>
</body>
</html>
Jul 23 '05 #2
McKirahan wrote:

[snip]
max += document.forms[0].elements[i].value.length;
I think the OP is talking about adding the values, not the number of
characters (which isn't necessarily the same as the number of bytes,
by the way).

If I'm correct, the line above should be altered to

max += +document.forms[0].elements[i].value;

though max wouldn't be an appropriate variable name. It would also be
sensible to check the format of each entry.
window.status = max;
Please don't play around with the status bar, or encourage others to
do so.

[snip]
<form onsubmit="return bytes()">
Rather than look-up the form reference in the validation function, why
not pass a reference to the form as an argument. It can be done here
using the 'this' operator, and in form controls using 'this.form'.
<input type="text" name="t001" value="" onblur="bytes()">
<input type="text" name="t002" value="" onblur="bytes()">


Validating on the blur event is usually a very bad idea. The main
reason is that's its a pain in the ass to be told /every time/ a
control loses focus that something is wrong. Especially if you're
navigating by keyboard and you're just trying to get to the value you
want to change. Moreover, if the focus is changed in code, it can lead
to an endless loop. Whilst that doesn't occur here, its a good reason
not to demonstrate the approach at all.

Leave the validation until submission.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Ivo
"Michael Winter" spoke of
... the number of characters (which isn't necessarily the same as
the number of bytes, by the way).


somebody asked in this newsgroup last week what the relation was between
bytes and characters, and I was very interested, but nobody answered. Do you
have an idea how to calculate one from the other?
window.status = max;


Please don't play around with the status bar, or encourage others to
do so.


I must say, I find this use of the statusbar relatively innocent. Not sure
about other browsers, but in IE it is the only window component that will
update its display while a javascript is running. I 'm sure they built it
like that to allow us to 'play with it' and see what our code is doing while
it is doing it. As long as it doesn't stand in the way of normal statusbar
behaviour.

--
Ivo
Jul 23 '05 #4
Ivo wrote:
"Michael Winter" spoke of
... the number of characters (which isn't necessarily the same as
the number of bytes, by the way).
somebody asked in this newsgroup last week what the relation was
between bytes and characters, and I was very interested, but nobody
answered. Do you have an idea how to calculate one from the other?


Not particularly, I'm just well aware of the difference. It would
depend on the encoding you're going to use. Encoding a Unicode string
with UTF-8 could result in sequences of one, two, three, or four bytes
for each character. UTF-16 can require one or two 16-bit code units
(so two or four bytes). Only once you decide on an encoding can you
determine the relationship.

[snip]
I must say, I find this use of the statusbar relatively innocent.


I wasn't trying to imply otherwise. However, I just think the status
bar isn't something to be messed with. If using alert boxes is too
intrusive, I dump output into a TEXTAREA (which is probably superior
to an alert in some respects).

Another reason for objecting in this context is that readers might not
understand that it's use is only for debugging purposes. That was the
point of the "encouragement" remark; readers might take it as an
indication that the status bar is a toy.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T. Wintershoven | last post by:
Hello, I have a few textboxes placed on a SSTab control devided over 3 tabs Is there a way to clear all textboxes in one time in stead of one by one. Someone told me "Use for each item...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
11
by: ian.davies52 | last post by:
Is there anything I can do about the apparent limit on the number of textboxes that have calculations as their control source on a form or report in ms-access? I have a query that pulls together...
4
by: Serdar C. | last post by:
hi there, i am writing a program with LOTS of textboxes in it.. my problem is when i try to clean the textboxes with foreach it gives error.. heres how my code looks like: private void ClearText...
0
by: Rathtap | last post by:
I am displaying a lot of data in textboxes on a web page. I would like to group related info such as Patient Address, Payer information etc in what is an equivalent of a VB 6 frame. In addition I...
2
by: Bruce F | last post by:
VBasic.net 2003 I have an array of 12 TextBoxes: Dim tbArray(4,3) As TextBox What I need is an Event Handler that will tell me in which TextBox a key was pressed. Can anyone enlighten me...
8
by: rdavis7408 | last post by:
I am attempting what I would think would be a simple calculation of the cost of traveling a single mile. But I can not figure this out. The following is my script. Any help would be appreciated. ...
0
by: Neo | last post by:
hi , i was trying to use textboxes and labels in a repeater control. If in case the value of the text boxes are changed then the changed textbox value and the label has to be added to two...
16
by: Alexio | last post by:
Am new to javascript. I need to format the currency in the as numbers are entered in textboxes. I can format the currency in the calculations and display in the totals but can not figure out how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.