469,306 Members | 1,872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

javascript: test a specific element of a passed form

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 (obj.@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="" >
</p>
<INPUT type="submit" name="submit" value=" Submit " class="btn">
</div>

</form>

Mar 18 '06 #1
3 1255
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.

Mar 19 '06 #2
Bi*************@yahoo.com 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 (obj.@BHFOR.value == " " )
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.

</div>
Closing div tag not opened. :-)

</form>


--
Rob
Mar 20 '06 #3
RobG wrote:
Bi*************@yahoo.com 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.

<URL:http://www.w3.org/TR/html4/index/attributes.html>
</div>


Closing div tag not opened. :-)


s/tag/element/
Regards,
PointedEars
Mar 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.