Skip navigation.

Sign up | Lost password? | Help

miscoded

the web is a hack

how to use or survive WebForms2

, , ,

EDIT: New and improved version of this blog post here:
http://dev.opera.com/articles/view/making-legacy-pages-work-with-web-forms/


Forms need validation. User-friendly, helpful, client-side validation. Plenty of validation examples exist, using JavaScript of various flavours.

The WebForms2 attempts to extend HTML4's form features and one of the objectives is to add support for common validation scenarios.


Being an early implementor of WebForms2 we've seen a few cases where WebForms2 attributes conflict with existing custom attributes (or runs into simple markup mistakes). Generally there are two cases of problems, either the built-in validation unexpectedly prevents form submits, or unexpected new properties confuse JavaScripts that look for custom ones.

Here are some suggestions for webmasters on how to handle problems:

Case: Opera's validation kicks in and prevents your form from submitting

Here you can be a fireman or an innovator - or both. To be a Fireman and just stamp out the problem, you can add a tiny touch of JavaScript to handle invalid events and sort out the issues. For example, to make Squidoo's login work, the fireman approach might be

document.addEventListener( 'invalid' , 
/* when an invalid event occurs, the user is trying to submit a form with invalid fields */
function(e){ 
  var input=e.target;
  if(input.validity.patternMismatch){ /* value crashed with pattern attribute */
     e.target.removeAttribute('pattern');
  }
  e.preventDefault(); /* do not show validation error */
  if(validate(e.target.form)) e.target.form.submit(); /* make sure form submits (Opera bug workaround) */

}, false );


To be an innovator, make sure your custom attributes match the definition in WF2. For example, the problem with

<input required="true" pattern="email" type="text" value="" tabindex="1"
 name="email_address" id="email_address"  />


is that the pattern attribute should be a regular expression.

<input required="true" pattern=".*@.*\..*" type="text" value="" tabindex="1"
 name="email_address" id="email_address"  />


(I know that's simplistic, I just don't want to divert into the endless debate about finding the perfect regexp for E-mail addresses..)

Now, the validation script needs to be updated to take that into account of course. First of all, it can "learn" not to validate forms in WF2-browsers since that is handled by the browser. For example on Squidoo, they have a function called validate that receives a form as its argument. They could write

function validate(form){
  if(form.checkValidity){ return; } // browser has built-in validation


..then rewrite the part that checks the "pattern" attribute for the other browsers to read the regexp directly from the pattern.

JS being confused by new built-in properties

FreeParking's domain lookup is broken in Opera 9. The reason is that the script finds input.min and input.max attributes, both are empty but since they are not null the script thinks the domain name is supposed to be a number.

The only real fix for that is to audit your custom attributes and make sure they do not conflict with WebForms2. The fireman could of course also check if a WF2-browser is used, meaning

if (e.numeric || (e.min != null) || (e.max != null))


becomes for example

if ( e.willValidate==null && (  e.numeric || (e.min != null) || (e.max != null) ) )


since if willValidate is defined the UA would handle min and max with its built-in validation. (Of course they could also just remove the null comparison since the empty strings will evaluate to false anyway - but they may want to be able to set an empty min or max attribute to trigger number validation..)

Launching OddPatch 0.1 Betaspot a subtle JS error here..

Comments

xErath 22. September 2006, 01:32

he reason is that the script finds input.min and input.max attributes, both are empty but since they are not null the script thinks the domain name is supposed to be a number.


As a survival policy, Opera could just ignore non numerical min/max attribute values.

hallvors 22. September 2006, 19:19

The problem is that WF2 defines those DOM properties, so now they exist on all HTMLInputElement objects even if there is nothing in the markup.

RobbieGee 4. October 2006, 23:47

Pfft, amateur... everyone knows the regexp for matching email addresses by rfc 822 is (?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \x00-\x1F]+(?:(?:
(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.
|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=
[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(
?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \
x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\
[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])
*(?:[^()<>@,;:\\".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z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\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[
\t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \x00
-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*))*
)?;\s*)

n00b!!! ;-P

hallvors 5. October 2006, 21:26

LOL. A regexp so long you could decorate toilet paper with it :smile:

tarquinwj 14. November 2006, 13:06

Think it would be easier to just write down every possible email address, and check against that list P:

Word-a-day toilet paper becomes email-address-a-day toilet paper.

FataL 9. April 2008, 16:16

I just stuck with Opera when implementing custom validation. Opera doesn't get attention to willValidate attribute set to false -- it wants validate fields anyway. :frown:
For other browsers I use WebForms 2 library and it works as expected. I just don't know how to fix it in Opera. See my test page.
BTW, is it known bug or I should report it?

FataL 9. April 2008, 16:29

Other two things related to WebForms 2 that I found broken in Opera are:

hallvors 10. April 2008, 17:24

FataL: I don't see WF2 saying anything about setting the willValidate attribute! As it is specified I think it's a sort of read only attribute informing your script or the UA that this element will be included in validation.

That also implies there is no way in WF2 to say from a script that "this element should not be considered for validation (even though it has validation attributes like required)". To avoid validation of elements you actually have to remove the validation attribute, for example input.removeAttribute('required').

I think you should bring this issue up with WHATWG or the HTML WG. The spec should be clarified.

FataL 10. April 2008, 19:10

Originally posted by hallvords:

I don't see WF2 saying anything about setting the willValidate attribute!

Hmmm... I would interpret following as willValidate attribute can be set by user via JavaScript:

each element in this list whose willValidate DOM attribute is true is checked for validity


Originally posted by hallvords:

That also implies there is no way in WF2 to say from a script that "this element should not be considered for validation (even though it has validation attributes like required)". To avoid validation of elements you actually have to remove the validation attribute, for example input.removeAttribute('required').

In this case I will lost validation of element "forever". I used willValidate set to false to temporary remove validation of some form field.
Also you may want to remove validation from not required field (allow enter text to number field, etc.)...

Originally posted by hallvords:

I think you should bring this issue up with WHATWG or the HTML WG. The spec should be clarified.

Shure. I will try to ask Hixie...

Update: I posted a question to WHATWG forum. Lets wait for the answer.

Update 2: Hmmmm... I see this the specs:

readonly - attribute - boolean - willValidate;

So, it is to read only. :frown:
Now I think we should disscuss this with WHATWG...

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies