javascript: test a specific element of a passed form

B

Bill_W_Stephens

I am modifying existing javascript code where the author appears to be
passing an entire form to a javascript function called Require(). I
want to add a new test: "is @BHFOR.value = blanks". Interestingly it
works in Firefox but not IE so I suspect that the syntax is not
correct.

Is there a better way to test a specific element of the form in this
case?

Here is an extract of the code:

<script language="javascript" type="text/javascript">
function Require(obj) {
for (var i=0; i<obj.length; i++) {
// a bunch of numeric validations that work now.
}

// my addition that causes script to fail
if ([email protected] == " " )
{
alert("You must enter current License Number");
return false;
}
</script>


<form name="form1" action="OP1704.pgm"
onSubmit="return Require(this)">

<p>Enter Current License Number.</p>
<INPUT type="text" name="@BHFOR" size="25" maxlength="25" value="" >
</p>
<INPUT type="submit" name="submit" value=" Submit " class="btn">
</div>

</form>
 
T

tim.n.hunt

Hiya

The program logic seems ok, looks like the problem is the @ symbol at
the start of the input elements name. Try BHFOR instead of @BHFOR.
 
R

RobG

(e-mail address removed) said on 19/03/2006 4:41 AM AEST:
I am modifying existing javascript code where the author appears to be
passing an entire form to a javascript function called Require(). I
want to add a new test: "is @BHFOR.value = blanks". Interestingly it
works in Firefox but not IE so I suspect that the syntax is not
correct.

Is there a better way to test a specific element of the form in this
case?

Here is an extract of the code:

<script language="javascript" type="text/javascript">

The language attribute is deprecated so remove it. Keep type.

function Require(obj) {

Function names starting with a capital letter are normally reserved for
constructors. No biggie, just a convention.

for (var i=0; i<obj.length; i++) {
// a bunch of numeric validations that work now.
}

// my addition that causes script to fail
if ([email protected] == " " )

The '@' symbol is your problem (see below). Use:

if (obj['@BHFOR'].value == " " )


or more formally:

if (obj.elements['@BHFOR'].value == " " )

{
alert("You must enter current License Number");
return false;
}
</script>


<form name="form1" action="OP1704.pgm"
onSubmit="return Require(this)">

<p>Enter Current License Number.</p>
<INPUT type="text" name="@BHFOR" size="25" maxlength="25" value="" >

Contrary to what you may have been told, the name attribute is CDATA so
you can put almost anything in there.

<URL:http://www.w3.org/TR/html4/interact/forms.html#adef-name-INPUT>


However, when using the name, if it starts with anything other than a
letter or contains anything other than letters and numbers, always use
square brackets and string literal to access it. i.e. don't use
formRef.@BHFOR use formRef['@BHFOR']. It's a good idea to always use
square brackets anyway.

</p>
<INPUT type="submit" name="submit" value=" Submit " class="btn">

There is no need to give the submit button a name, so don't. And
especially don't call it 'submit' as it will override the form's submit
method, causing an error (something like 'form.submit is not a
function') if you try to call it.


Closing div tag not opened. :)
 
T

Thomas 'PointedEars' Lahn

RobG said:
(e-mail address removed) said on 19/03/2006 4:41 AM AEST:
[...]
<INPUT type="text" name="@BHFOR" size="25" maxlength="25" value="" >

Contrary to what you may have been told, the name attribute is CDATA [...]

The value of the `name' attribute of `input' elements is indeed
of type CDATA. Most `name' attribute values are. Not all.

Closing div tag not opened. :)

s/tag/element/


Regards,
PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top