"disabled" property for form data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Hannon

    #1

    "disabled" property for form data

    I have been researching the "disabled" property for form data and
    wanted to know:

    - Does the disabled property supress the form item from being submitted?
    - Can the disabled property be applied when the submit button is
    pressed?

    I am designing a PayPal order page that will have 4 items, each with a
    3-character text box where the customer will enter quantity. The
    default value will be "0." I want to be able to test whether the
    quantity is greater than 0. If it isn't, then I want to disable the
    form fields attached to that item so that they don't even appear in the
    final statement.

    This is my idea, let me know if this will work the way I want:

    function tally(){
    if(document.ord erform.quantity .value == 0){
    document.orderf orm.quantity.di sabled = true;
    } else{
    document.orderf orm.quantity.di sabled = false;
    }
    // repeat for next 3 items
    //
    // the rest of the tally code continues below
    }
  • Martin Honnen

    #2
    Re: "disabled& quot; property for form data



    Mark Hannon wrote:
    [color=blue]
    > I have been researching the "disabled" property for form data and
    > wanted to know:
    >
    > - Does the disabled property supress the form item from being submitted?[/color]

    Yes, according to the HTML 4 specification it should and the browser I
    know of that support the disabled attribute (IE 4+, Netscape 6/7,
    Mozilla, Opera 7) follow that specification:
    http://www.w3.org/TR/html4/interact/...ssful-controls



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Michael Winter

      #3
      Re: "disabled& quot; property for form data

      On Thu, 19 Aug 2004 17:09:15 GMT, Mark Hannon <uglyheads@eart hlink.net>
      wrote:
      [color=blue]
      > I have been researching the "disabled" property for form data and
      > wanted to know:
      >
      > - Does the disabled property supress the form item from being submitted?[/color]

      Yes. From the HTML Specification, section 17.13.2 - Successful controls:

      A successful control is "valid" for submission. Every successful
      control has its control name [p.220] paired with its current value
      as part of the submitted form data set. A successful control must be
      defined within a FORM element and must have a control name.

      However:
      Controls that are disabled cannot be successful.
      [Remaining conditions omitted]
      [color=blue]
      > - Can the disabled property be applied when the submit button is
      > pressed?[/color]

      As far as I know, yes.
      [color=blue]
      > I am designing a PayPal order page that will have 4 items, each with a
      > 3-character text box where the customer will enter quantity. The default
      > value will be "0." I want to be able to test whether the quantity is
      > greater than 0. If it isn't, then I want to disable the form fields
      > attached to that item so that they don't even appear in the final
      > statement.[/color]

      Don't forget that users can circumvent JavaScript validation
      intentionally, or without even knowing. You should always validate on the
      server in exactly the same way as you do on the client. Client-side
      validation should just be a shortcut. This is especially important when it
      comes to money, as I'm sure you can imagine.
      [color=blue]
      > This is my idea, let me know if this will work the way I want:
      >
      > function tally(){
      > if(document.ord erform.quantity .value == 0){
      > document.orderf orm.quantity.di sabled = true;
      > } else{
      > document.orderf orm.quantity.di sabled = false;
      > }[/color]

      That's fine, though it can be shortened.

      Assuming that you're calling this from the form's submit event listener:

      /* f - A reference to the form.
      *
      * Return true if the form validates, false otherwise.
      */
      function tally(f) {
      /* Get a reference to the collection of form controls. */
      var e = f.elements;

      /* Check that 'quantity' is a valid number first. */

      /* If 'quantity' is zero (0), disable the control. */
      e['quantity'].disabled = e['quantity'].value == 0;

      /* Check remainder of form. */
      }

      <form ... onsubmit="retur n tally(this);">

      You could even save a reference to the quantity form control if you use it
      a lot.

      As I said in the comments above, you should also check that the quantity
      value is a valid, positive number using regular expressions. See Dr
      Stockton's web page for more information:

      <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Lee

        #4
        Re: &quot;disabled& quot; property for form data

        Mark Hannon said:[color=blue]
        >
        >I have been researching the "disabled" property for form data and
        >wanted to know:
        >
        >- Does the disabled property supress the form item from being submitted?
        >- Can the disabled property be applied when the submit button is
        >pressed?
        >
        >I am designing a PayPal order page that will have 4 items, each with a
        >3-character text box where the customer will enter quantity. The
        >default value will be "0." I want to be able to test whether the
        >quantity is greater than 0. If it isn't, then I want to disable the
        >form fields attached to that item so that they don't even appear in the
        >final statement.[/color]

        Save this test case to your desktop,
        open it in your browser and see for yourself.

        <html>
        <body>
        <form>
        <input name="alpha" value="a">
        <input name="beta" value="b">
        <input value="submit"
        type="submit"
        onclick="this.f orm.beta.disabl ed=true">
        </form>
        </body>
        </html>

        However, what about people who don't have javascript enabled?
        [color=blue]
        > if(document.ord erform.quantity .value == 0){
        > document.orderf orm.quantity.di sabled = true;
        > } else{
        > document.orderf orm.quantity.di sabled = false;
        > }[/color]


        That could be written as:
        document.orderf orm.quantity.di sabled=!documen t.orderform.qua ntity.value;

        Comment

        • Michael Winter

          #5
          Re: &quot;disabled& quot; property for form data

          On 19 Aug 2004 10:23:38 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:

          [snip]
          [color=blue][color=green]
          >> if(document.ord erform.quantity .value == 0){
          >> document.orderf orm.quantity.di sabled = true;
          >> } else{
          >> document.orderf orm.quantity.di sabled = false;
          >> }[/color]
          >
          > That could be written as:
          > document.orderf orm.quantity.di sabled=
          > !document.order form.quantity.v alue;[/color]

          No, it couldn't. When converting a string to a boolean, it is the length
          that's the deciding factor, not the value. Either convert it to a number
          first, or assign the result of a comparison with zero.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Lee

            #6
            Re: &quot;disabled& quot; property for form data

            Michael Winter said:[color=blue]
            >
            >On 19 Aug 2004 10:23:38 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:
            >
            >[snip]
            >[color=green][color=darkred]
            >>> if(document.ord erform.quantity .value == 0){
            >>> document.orderf orm.quantity.di sabled = true;
            >>> } else{
            >>> document.orderf orm.quantity.di sabled = false;
            >>> }[/color]
            >>
            >> That could be written as:
            >> document.orderf orm.quantity.di sabled=
            >> !document.order form.quantity.v alue;[/color]
            >
            >No, it couldn't. When converting a string to a boolean, it is the length
            >that's the deciding factor, not the value. Either convert it to a number
            >first, or assign the result of a comparison with zero.[/color]

            Yes. I had misread it as if he was testing for an empty field.

            Comment

            Working...