Re: Valid form element names? How to use ID instead of name? Processing form data

Discussion in 'HTML' started by Michael Winter, Mar 6, 2004.

  1. On Sat, 06 Mar 2004 12:32:21 GMT, Grahammer <postmaster@127.0.0.1> wrote:

    [Follow-ups set to alt.html]

    > Sorry for the newbie questions...
    >
    > Is there a link somewhere that shows what defines a valid form object
    > (element) name or ID?


    The HTML Specification defines the accepted characters for all attribute
    values. The id attribute is of type ID, and the name attribute for form
    controls is of type CDATA. From the Specification:

    ID and NAME[1] tokens must begin with a letter ([A-Za-z]) and may be
    followed by any number of letters, digits ([0-9]), hyphens ("-"),
    underscores ("_"), colons (":"), and periods (".").

    and

    CDATA is a sequence of characters from the document character set
    and may include character entities. User agents should interpret
    attribute values as follows:

    - Replace character entities with characters,
    - Ignore line feeds,
    - Replace each carriage return or tab with a single space.

    User agents may ignore leading and trailing white space in CDATA
    attribute values (e.g., " myval " may be interpreted as "myval").
    Authors should not declare attribute values with leading or trailing
    white space.

    > I want to do some automated form processing, which means referencing form
    > objects in javascript and vbscript so I need to choose my element names
    > and ID's carefully.
    >
    > For example...
    > <form action="somepage.htm" name="MyForm" method="post">
    > <input type="text" name="UserInput" id="textbox1">
    > etc
    > ...would be valid, because in my scripting I could say:
    > if (MyForm.UserInput.value == "something") { ...


    You should fully reference properties, so that

    MyForm.UserInput.value

    becomes

    document.MyForm.UserInput.value

    > but what about:
    >
    > <input type="text" name="UserInput$" id="textbox1">
    > <input type="text" name="UserInput_" id="textbox1">
    > <input type="text" name="UserInput+" id="textbox1">


    For the folks at alt.html:

    When name and id are used together in form controls, should they match? I
    know that in other elements (A, FORM, IMG, etc), they must be identical
    when used together, but does this apply to form controls, too?

    > ...also, could I use the ID like this since the input box has both a name
    > and ID?
    > if (MyForm.textbox1.value == "something") { ...


    Some browsers don't allow shortcut references when an id attribute is
    involved. In JavaScript, you can use the collection syntax:

    document.forms['MyForm'].elements['textbox1'].value

    You can also use this syntax where names might produce conflicts in
    scripting languages. For example:

    <input name="line[0]" ...>

    might be part of several INPUT elements produced by a server-side script.
    Shortcut referencing, shown below, will not work as expected.

    document.someForm.line[0]

    The scripting engine will expect 'line' to be a collection or array and
    will try to access the first element as opposed to accessing the element
    with the name 'line[0]'. Using the collection syntax will avoid this:

    document.someForm.elements['line[0]']

    [snipped VBScript]

    Hope that helps,
    Mike


    [1] The NAME type is not to be confused with the name attribute. The name
    attribute is of type CDATA.

    --
    Michael Winter
    lid (replace ".invalid" with ".uk" to reply)
    Michael Winter, Mar 6, 2004
    #1
    1. Advertising

  2. On Sat, 06 Mar 2004 15:06:50 GMT, Grahammer <postmaster@127.0.0.1> wrote:

    > "Michael Winter" <> wrote in message
    > news:eek:...
    >
    >> On Sat, 06 Mar 2004 12:32:21 GMT, Grahammer <postmaster@127.0.0.1>
    >> wrote:
    >>
    >> [Follow-ups set to alt.html]

    >
    > ??? I've seen this, but I don't know how Outlook Express can reply to a
    > list of groups with followups only to a single one, like you did here.


    When you compose a message, there should be a menu item along the lines of
    "Show all headers" in the View menu (it might be in a sub-menu). If you
    select that, several new header lines will appear along with Subject,
    Newsgroup, and the like. Some of them you won't ever use, but the
    follow-up field should be amongst them.

    >> > Is there a link somewhere that shows what defines a valid form object
    >> > (element) name or ID?

    >>
    >> The HTML Specification defines the accepted characters for all attribute
    >> values. The id attribute is of type ID, and the name attribute for form
    >> controls is of type CDATA. From the Specification:
    >>
    >> ID and NAME[1] tokens must begin with a letter ([A-Za-z]) and may be
    >> followed by any number of letters, digits ([0-9]), hyphens ("-"),
    >> underscores ("_"), colons (":"), and periods (".").

    >
    > Thanks! This is what I was looking for (or a link)


    Sorry, I should have also given a link. The specific section I referenced
    can be found here:

    http://www.w3.org/TR/html4/types.html#h-6.2

    The Specification as a whole can be found here:

    http://www.w3.org/TR/html4

    [snipped full object references]

    >> When name and id are used together in form controls, should they match?
    >> I know that in other elements (A, FORM, IMG, etc), they must be
    >> identical when used together, but does this apply to form controls,
    >> too?

    >
    > My editor is Visual Studio.Net and it does NOT match ID with NAME at
    > all! It insists on adding an ID value to all my input tags.


    According to the specification, the elements A, APPLET, FORM, FRAME,
    IFRAME, IMG, and MAP should use the id attribute (my impression is that
    name is being phased out), but when the name attribute is used for
    backward compatibility, it must match the id value.

    http://www.w3.org/TR/html4/struct/links.html#h-12.2.3

    I don't think it applies to controls within forms, but I thought I'd ask
    whilst I'm posting to alt.html.

    [snipped collection syntax]

    >> Hope that helps,

    >
    > Definately... and apologies for the scripting questions in the HTML
    > group, but they were related and I thought helped clarify my
    > questions.


    The first part (characters within attributes) was strictly HTML, but the
    latter material did have reprocussions for scripting.

    Mike

    --
    Michael Winter
    lid (replace ".invalid" with ".uk" to reply)
    Michael Winter, Mar 6, 2004
    #2
    1. Advertising

  3. Michael Winter wrote:

    > When name and id are used together in form controls, should they match? I
    > know that in other elements (A, FORM, IMG, etc), they must be identical
    > when used together, but does this apply to form controls, too?


    They don't need to match. In fact, in some cases they MUST NOT match. For
    example, consider adding an id attribute to both of these:

    <input type="radio" name="opt" value="Yes">
    <input type="radio" name="opt" value="No">

    In this case, the id MUST NOT match the name. Why? Because the names for
    each control MUST be the same, and you're not allowed to have two elements
    with the same id.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Mar 6, 2004
    #3
  4. On Sat, 06 Mar 2004 19:48:02 +0000, Toby A Inkster
    <> wrote:

    > Michael Winter wrote:
    >
    >> When name and id are used together in form controls, should they match?
    >> I know that in other elements (A, FORM, IMG, etc), they must be
    >> identical when used together, but does this apply to form controls,
    >> too?

    >
    > They don't need to match. In fact, in some cases they MUST NOT match. For
    > example, consider adding an id attribute to both of these:
    >
    > <input type="radio" name="opt" value="Yes">
    > <input type="radio" name="opt" value="No">
    >
    > In this case, the id MUST NOT match the name. Why? Because the names for
    > each control MUST be the same, and you're not allowed to have two
    > elements with the same id.


    :) Of course. Should have thought about that case.

    Thank you.

    Mike

    --
    Michael Winter
    lid (replace ".invalid" with ".uk" to reply)
    Michael Winter, Mar 6, 2004
    #4
  5. Re: Valid form element names? How to use ID instead of name?Processing form data

    Michael Winter <> writes:


    > For the folks at alt.html:
    >
    > When name and id are used together in form controls, should they
    > match?


    No. They don't have to, and in practice they should probbly be made
    not to match. The "name" attribute specifies the control name, which
    is the name used when submitting the form. The name should be chosen
    to match this functionality. Since you can have more than one form
    control with the same control name (in the same form or in different
    forms), they can not all have the same "id" attribute.

    > I know that in other elements (A, FORM, IMG, etc), they must be
    > identical when used together, but does this apply to form controls,
    > too?


    The specification doesn't say that they must be equal when both are
    used. It explicitly says that for those other elements, but omits
    speaking about form controls (or the "param" or "meta" elements).
    That can only mean that that specific rule does not apply to
    form controls.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Mar 6, 2004
    #5
    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.

Share This Page