473,395 Members | 1,978 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,395 software developers and data experts.

help with data validation

I'm trying to use Greasemonkey to add form validation to some Web pages
(whose construction is otherwise out of my control). To boil it down to
a simple test case, the combination of the original HTML and my script
results in something like...

<html>
<head>
<title>Validator test</title>
<script type="text/javascript"><!--
function Loaded () {
var theField = document.getElementById ('field1') ;
theField.addEventListener ('change', validCk, false) ;
}
function validCk (e) {
if (!/([a-c])/.test (this.value) ) {
alert ('You must enter a, b, or c in this field.') ;
e.preventDefault () ;
e.stopPropagation () ;
this.focus () ;
return false ;
}
}
// --></script>
</head>
<body onload="Loaded();">
<form action="http://www.example.com/submitted/">
<input id="field1" maxlength="1" size="1" type="text" />
<input id="submit1" type="submit" value="Go" />
</form>
</body>
</html>

Steps to reproduce the problem:
- Cut and paste the above HTML into a file, and load it into Firefox.
- Enter "Q" in the text field.
- Without hitting the Tab key or otherwise exiting the text field, click
the "Go" button.

Expected result:
- The warning box pops up, and when the user dismisses it, the cursor is
back in the text field.

Observed result:
- The warning box pops up, but before the user dismisses it, the form is
submitted anyway.

Now, I understand that preventDefault() and stopPropagation() are
affecting field1 and not submit1, but why not? Shouldn't canceling an
event cancel ALL the effects of that event? More to the point, how can
I ensure that the "submit" effects ARE canceled, and how can I do it
when the actual page could have several dozen submit buttons, in guises
such as...

<a href="javascript:forms[0].submit()">Go</a>

<a href="javascript:__doPostBack('foo2','bar2')">Go</a>

<a onclick="__doPostBack('foo3','bar3')">Go</a>

<select onchange="javascript:__doPostBack('foo4','bar4')">
<option>!Go</option>
<option>Go!</option>
</select>

<a href="javascript:void(0)" id="anchor5">Go<script
type="text/javascript"><!--
var a5 = document.getElementById ('anchor5') ;
a5.addEventListener ('click', function () {
__doPostBack ('foo5', 'bar5')
}, false) ;
--</script></a>

....???
Aug 5 '08 #1
6 1265
Jim Wooseman wrote:
var theField = document.getElementById ('field1') ;
theField.addEventListener ('change', validCk, false) ;
}
function validCk (e) {
if (!/([a-c])/.test (this.value) ) {
alert ('You must enter a, b, or c in this field.') ;
e.preventDefault () ;
See
http://www.w3.org/TR/2000/REC-DOM-Le...ngs-htmlevents,
the change event is not cancelable so that preventDefault() does not help.

More to the point, how can
I ensure that the "submit" effects ARE canceled, and how can I do it
when the actual page could have several dozen submit buttons, in guises
such as...
If you want to cancel form submission by a submit button then you should
add an event listener for the submit event to the form itself. The
submit event can be cancelled so that listener should call
e.preventDefault() if you don't want the form to be submitted.

However note that calling the submit method of a form does not raise the
submit event.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 5 '08 #2
Martin Honnen wrote:
However note that calling the submit method of a form does not raise the
submit event.
Then that does me no good, because 90% of the "Go" links on this page
call submit(), either directly or via __doPostBack(). :(
Aug 6 '08 #3
Jim Wooseman wrote:
Martin Honnen wrote:
>However note that calling the submit method of a form does not raise the
submit event.

Then that does me no good, because 90% of the "Go" links on this page
call submit(), either directly or via __doPostBack(). :(
Make them submit buttons and extend the form to include them. This is also
the accessible approach.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Aug 6 '08 #4
On Aug 5, 8:04*pm, Jim Wooseman <dont.need...@stinkin.spamwrote:
Martin Honnen wrote:
However note that calling the submit method of a form does not raise the
submit event.

Then that does me no good, because 90% of the "Go" links on this page
call submit(), either directly or via __doPostBack(). :(
That's what happens when you use incompetently designed frameworks
(e.g. ASP.NET.)
Aug 6 '08 #5
Thomas 'PointedEars' Lahn wrote:
Make them submit buttons and extend the form to include them. This is also
the accessible approach.
That _would_ be The Right Thing to do, but it's also a lot of work for a
Greasemonkey script.
Aug 7 '08 #6
David Mark wrote:
That's what happens when you use incompetently designed frameworks
(e.g. ASP.NET.)
I know, but I didn't pick it, I just use the site.
Aug 7 '08 #7

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

Similar topics

2
by: qsweetbee | last post by:
I have a form(fAddUsers) in my database. It is continue form for data entry. Some fields are required fields. Some are optional fields. There is 1 particular filed(TokenExpirationDate)on the form...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
2
by: Dnna | last post by:
I have a table which is bound to an Internet Explorer XML data island. I'm using ASP.NET's client-side validators for an input field in the table. The problem is that if the input fields are in...
6
by: serge calderara | last post by:
Dear all, I have read that ASP.NET does double user input validation of control when they are place on the page. Once on teh client side and again from server side right ? Could explain how...
7
by: phillip.s.powell | last post by:
Now I have another SQL query for MySQL I can't figure out!! This is overwhelming me completely and I also must have this figured out today and I can't figure it out!! UPDATE student_db.student...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
4
by: AMBLY | last post by:
Hi Hope you can help me with this I run Access2000 on XP Form has 3 fields: cboSTATUS - which can be set via Limited value List A B or Complete. Default is set as A. cboTHIS - which can...
4
by: Melissa | last post by:
I currently have VBA written to export query results into an Excel file. That file is then formatted using the code below. The problem I'm having is that it keeps throwing Error 91 (Object...
6
by: smk17 | last post by:
I've spent the last few minutes searching for this question and I found an answer, but it wasn't quite what the client wanted. I have a simple online form where the user needs to fill out five...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...

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.