Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of LTjake (4001)

Tuesday March 18, 2003
02:06 PM

Tilde Terror

[ #11095 ]

We have a batch of ASPs at work that we affectionately (well, not really) call the "survey generator." They were created by a co-op student a while back, before I even started working here. I've been lucky enough to have avoided it -- until now.

I was trying to apply some simple javascript form validation to it, but for some reason, it kept throwing an error. So, I re-read my references to make sure I was doing this properly. All seemed well.

It turns out, that you can't have a tilde in the name of a form element. Now, I'm not sure who's brilliant idea it was to use tildes in the first place, but it sure puts a wrinkle in my plans. Here's some sample code to show you what's up:

    <title>Tilde Test</title>
    <script language="javascript">
function checker() {
*    This next line generates an error due to the
*    tilde in the name. Evil.
*    Line: 15
*    Char: 20
*    Error: Expected ')'

//    if (document.testform.tilde~tilde.checked) {

    if (document.testform.elements[0].checked) {
    else {
    return false;
<form name="testform">
    <p><input type="checkbox" name="tilde~tilde" /> Check Me</p>
    <p><input type="submit" onclick="return checker();" /></p>

So, now everything has to be referenced by element number (elements[x]) instead of by name. But, with names like 2~txt~3, is it really any worse?

Update: Hey... vsergu tells me that

if (document.testform['tilde~tilde'].checked) {

should work. A quick test shows that it does. Thanks! Why don't people use this method? Everywhere i read it's either formname.elementname or forms[x].elements[y].

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • You should be able to use form_name['weird~element~name'] instead of resorting to numbers. Generally in JavaScript you can refer to properties either the normal way (with a dot) or as elements of an associative array. If the property names aren't usable as identifiers then the associative-array way is the only way.
  • Now, I'm not sure who's brilliant idea it was to use tildes in the first place, but it sure puts a wrinkle in my plans.

    Brilliant pun.

    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers