javascript: test a specific element of a passed form

Discussion in 'Javascript' started by Bill_W_Stephens@yahoo.com, Mar 18, 2006.

  1. Guest

    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 ( == " " )
    {
    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, 2006
    #1
    1. Advertising

  2. Guest

    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, 2006
    #2
    1. Advertising

  3. RobG Guest

    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 ( == " " )


    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
    RobG, Mar 19, 2006
    #3
  4. RobG wrote:

    > 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
    Thomas 'PointedEars' Lahn, Mar 20, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    398
    Vidar Petursson
    May 29, 2004
  2. dee
    Replies:
    9
    Views:
    493
    Joseph Byrns
    Apr 15, 2005
  3. Anand
    Replies:
    2
    Views:
    887
    Anand
    Sep 11, 2003
  4. mazdotnet
    Replies:
    2
    Views:
    385
    Alexey Smirnov
    Oct 2, 2009
  5. Hunt Jon
    Replies:
    1
    Views:
    96
    Patrick Doyle
    Dec 15, 2008
Loading...

Share This Page