Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > help with data validation

Reply
Thread Tools

help with data validation

 
 
Jim Wooseman
Guest
Posts: n/a
 
      08-05-2008
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>

....???
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-05-2008
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/
 
Reply With Quote
 
 
 
 
Jim Wooseman
Guest
Posts: n/a
 
      08-06-2008
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().
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-06-2008
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
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      08-06-2008
On Aug 5, 8:04*pm, Jim Wooseman <(E-Mail Removed)> 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().


That's what happens when you use incompetently designed frameworks
(e.g. ASP.NET.)
 
Reply With Quote
 
Jim Wooseman
Guest
Posts: n/a
 
      08-07-2008
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.
 
Reply With Quote
 
Jim Wooseman
Guest
Posts: n/a
 
      08-07-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying a Validation Error in a Validation Summary Control Lucas Tam ASP .Net 2 02-26-2004 07:49 PM
ASP.NET Web Forms Validation Controls are Server-Side or Client-Side Validation? Matt ASP .Net 14 01-30-2004 09:15 AM
Web form validation vs object validation Colin Basterfield ASP .Net 1 11-29-2003 12:10 AM
validation summary doesnt display when there's client-side validation Libs ASP .Net 0 06-25-2003 03:05 PM
Re: only custom validation control does server side validation? Colin Mackay ASP .Net 0 06-25-2003 07:54 AM



Advertisments