PDA

View Full Version : Regex does not work in Opera 7


Philip M
05-31-2003, 04:03 AM
The following works fine in MSIE and Netscape, but not in Opera 7.

<td><input type="text" size="8" maxlength="8" name="OrderQty1" onblur = "invchars(this)"></td>

<td><input type="text" size="74" maxlength="74" name="OrderDesc1" onblur = "if (this.value!=''){if(/^\s*$/.test(OrderQty1.value)){alert ('You have not entered the quantity of this item that you require! ')}}; format(this);"></td>

The idea is obviously that unless the user enters a quantity he is warned of the need to do so. That is, if text is entered into the Description with nothing in the Quantity fields.

The function format() is not the problem - it works fine but Opera chokes on the if..... section.

Any clues, please?

liorean
05-31-2003, 04:30 AM
The problem is that you use a proprietary way of referencing a field. Use DOM0 or DOM instead of the IE shortcut.

OrderQty1.value -->
this.form.elements['OrderQty1'].value

Philip M
05-31-2003, 08:02 AM
Thank you again! Tusen tak!

But I am not sure that I understand it!

The following works fine in all browsers:-


<td><input type="text" size="5" maxlength="2" name="IssueNum" onblur="if(/\D/g.test(this.value)){alert('Only a one- or two-digit number is valid in this box. '); this.value=''; this.focus;</td>

Could you explain the difference for me? Sorry if I am a bit slow on the uptake.

liorean
05-31-2003, 12:34 PM
It's simple, really:

In a form field, you have some keywords that you can use:
this to refer to the element itself
this.form to refer to the form the element is in
this.form.elements to refer to the elements collection of the same form.

So, when you use this.value, you access the value of the field the event handler is placed on. However, when you want to refer to another element you must either use the document object and go from there, or use the this.form.elements collection to reach the field in question.


Oh, and your spelling is a bit off there - Tusen (thousand) tack (thanks) :thumbsup:

cheesebagpipe
05-31-2003, 06:39 PM
liorean...

afaik, referencing form element names as variables within form element event handlers isn't IE-proprietary, it's enabled by the 'upside-down' scope chain of the handler (Function obj-->Element obj-->Form obj-->Document obj-->Window obj). Does this not work in Opera (& where else)? Don't have it handy and I'd really like to know....tia

liorean
05-31-2003, 06:47 PM
No, that is the object chain. The scope chain is just global(window)->document->form field. This is because the event is handled as were it called from a method of the document, that in turns called the method of the form field.

[:Edit:

Correction. I was wrong about this, it does as you said.]

cheesebagpipe
05-31-2003, 06:56 PM
OK, not following it. How am I misunderstanding this: :confused:

http://web.eecs.tufts.edu/notes/js_events.php3

liorean
05-31-2003, 07:37 PM
Having read up a bit on it (and done some quick-and-dirty testing), I better retract that statement. It does indeed follow the entire document structure. However, it should still not work, because the nameorid is not a variable accessible from anywhere in the scope chain. It is, rather, a property of the object the scope corresponds to in the instance that the scope is the form element. That browsers allow you to simply use the nameorid is simply allowing for laziness the same way using document.myForm.myElement is allowed as an alternative to document.form['myForm'].elements['myElement'].

If you report it to Opera though, they might fix it for the next release.