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

Form Validation Question

I don't know very much yet and I'm looking to do some form validation on
20 form fields that accept numbers. I would like to alert the user if
the combined values of any of the fields is greater than 40 (It's a 40
point survey).

Should I loop thru all the form fields when they tab out of each one to
check this (onblur)? I'm thinking it might be nice to let them know how
far over they are in the alert as well.

Code snip appreciated.

Thanks,
Frank

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
8 1236
You may display a text of the total point somewhere and the onblur
event will add to that amount and check if it exceeds 40. Then you do
not need to loop through fields. Just remember to implement the adding
and subtracting of that total amount correctly.

Jul 23 '05 #2
You may display a text of the total point somewhere and the onblur
event will add to that amount and check if it exceeds 40. Then you do
not need to loop through fields. Just remember to implement the adding
and subtracting of that total amount correctly.

Jul 23 '05 #3

"Jacky" <ja******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
You may display a text of the total point somewhere and the onblur
event will add to that amount and check if it exceeds 40. Then you do
not need to loop through fields. Just remember to implement the adding
and subtracting of that total amount correctly.


and watch out for the shift-tab.
The user could tab backwards after filling out part of the form
I would recomment looping thru all each time.
Also watch out for NaN entries. If you're coding for numbers, you can be
sure the user will be typing the Gettysberg address into your text box -
or - paste a chapter of War and Peace from another website.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.818 / Virus Database: 556 - Release Date: 12/17/2004
Jul 23 '05 #4

Frank Bishop wrote:
I don't know very much yet and I'm looking to do some form validation on 20 form fields that accept numbers. I would like to alert the user if
the combined values of any of the fields is greater than 40 (It's a 40 point survey).

Should I loop thru all the form fields when they tab out of each one to check this (onblur)? I'm thinking it might be nice to let them know how far over they are in the alert as well.

Code snip appreciated.

Thanks,
Frank

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<style type="text/css">

body {
font-size: 100%;
}
input.n {
width: 50px;
font: normal 70% tahoma;
text-align: center;
}
div#readout {
width: 240px;
height: 16px;
font: normal 70% tahoma;
text-align: center;
padding: 2px;
margin-left: 40px;
border: 1px #000 solid;
background: buttonface;
}
div#warning {
visibility: hidden;
width: 240px;
height: 16px;
font: normal 70% tahoma;
color: #f00;
text-align: center;
padding: 2px;
margin-top: 10px;
margin-left: 40px;
border: 1px #000 solid;
background: #ff0;
}

</style>
<script type="text/javascript">
//<![CDATA[

function set_fields()
{

function summit()
{
var i = 0,
el,
n,
b,
sum = 0;
while (el = els.item(i++))
if (isNaN(n = Number(el.value)))
{
el.value = 'invalid';
}
else sum += n;
var t = document.createTextNode('Combined value: ');
var s = document.createTextNode(sum);
el = document.getElementById('readout');
while (el.hasChildNodes())
el.removeChild(el.lastChild);
el.appendChild(t);
n = document.createElement('strong');
n.appendChild(s);
el.appendChild(n);
el = document.getElementById('warning');
if (sum > 40)
{
t = 'You have exceeded the allowed value by ' + (sum - 40) + ' !';
n = document.createTextNode(t);
while (el.hasChildNodes())
el.removeChild(el.lastChild);
el.appendChild(n);
el.style.visibility = 'visible';
}
else el.style.visibility = 'hidden';
}

var i = 0,
el,
els = document.getElementsByTagName('input');
while (el = els.item(i++))
if (/\bn\b/.test(el.className))
el.onchange = summit;
}

onload = set_fields;

//]]>
</script>
</head>
<body>
<form>
<ol>
<li><input id="n1" class="n" type="text" name="n1" value="" /></li>
<li><input id="n2" class="n" type="text" name="n2" value="" /></li>
<li><input id="n3" class="n" type="text" name="n3" value="" /></li>
<li><input id="n4" class="n" type="text" name="n4" value="" /></li>
<li><input id="n5" class="n" type="text" name="n5" value="" /></li>
<li><input id="n6" class="n" type="text" name="n6" value="" /></li>
<li><input id="n7" class="n" type="text" name="n7" value="" /></li>
<li><input id="n8" class="n" type="text" name="n8" value="" /></li>
<li><input id="n9" class="n" type="text" name="n9" value="" /></li>
<li><input id="n10" class="n" type="text" name="n10" value="" /></li>
<li><input id="n11" class="n" type="text" name="n11" value="" /></li>
<li><input id="n12" class="n" type="text" name="n12" value="" /></li>
<li><input id="n13" class="n" type="text" name="n13" value="" /></li>
<li><input id="n14" class="n" type="text" name="n14" value="" /></li>
<li><input id="n15" class="n" type="text" name="n15" value="" /></li>
<li><input id="n16" class="n" type="text" name="n16" value="" /></li>
<li><input id="n17" class="n" type="text" name="n17" value="" /></li>
<li><input id="n18" class="n" type="text" name="n18" value="" /></li>
<li><input id="n19" class="n" type="text" name="n19" value="" /></li>
<li><input id="n20" class="n" type="text" name="n20" value="" /></li>
</ol>
</form>
<div id="readout"></div>
<div id="warning"></div>
</body>
</html>

Just a rough approximation...

Jul 23 '05 #5
Oops....meant to do this:

function summit()
{
var i = 1,
el,
n,
b,
sum = 0;
while (el = document.getElementById('n' + i++))
.........and so on

Jul 23 '05 #6
On 17 Dec 2004 21:09:33 -0800, Jacky <ja******@gmail.com> wrote:

Please quote relevant text from the previous article when replying to a
post.
You may display a text of the total point somewhere and the onblur event
will add to that amount and check if it exceeds 40.


The blur event isn't usually suitable for validation, particularly if
you're going to add alerts. Use the change event instead.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7
Thanks Rob for this great code I can build from. Thanks to all for
suggestions. I'll be able to use this example for a lot of my form
creations.

Sincerely,
Frank
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
JRS: In article <41**********@127.0.0.1>, dated Fri, 17 Dec 2004
22:45:35, seen in news:comp.lang.javascript, Frank Bishop
<fb*****@viper.com> posted :
I don't know very much yet and I'm looking to do some form validation on
20 form fields that accept numbers. I would like to alert the user if
the combined values of any of the fields is greater than 40 (It's a 40
point survey).


For each field, let the onChange event check that it contains no non-
digit; if it fails, alert and set focus back; if it passes, add up all
the fields (interpret "" as "0") and show them in a readonly type-text
control. If that total is too big, you could disable the Submit button.
A field known to contain no non-digit can be converted to a number with
a unary + sign : N = +field.value .

OK = !/\D/.test(field.value)

You may wish, or not need, to trim leading & trailing spaces; see FAQ.

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9

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

Similar topics

11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
6
by: Drew | last post by:
I've already created a simple method of ensuring that all form feilds are filled out before the form is submitted to an ASP page for records to be added to the data base. (Sorry about the...
2
by: Tim Mills | last post by:
The following code asks the user to sumbit a name, email address, and some text for a quotation via a FORM. I have written a javascript function to evaluate the fields in the form and pop-up a...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
1
by: Colin Basterfield | last post by:
Hi, I have a web form which takes daily sales totals, both counts and monetary value and is done on a weekly basis, so on a Monday morning the User would enter these totals. Each total has a...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
18
by: Axel Dahmen | last post by:
Hi, trying to submit an ASPX form using the key (using IE6) the page is not submitted in my web project. Trying to debug the pages' JavaScript code I noticed that there's some ASP.NET client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.