Form trap problem

Discussion in 'Javascript' started by Garry Jones, Oct 5, 2006.

  1. Garry Jones

    Garry Jones Guest

    I want to ensure the users choose an item from a drop down list.
    The preselected value is "Not Chosen"

    Using script I cloned this from other working code, but it is broken
    somewhere.

    function checkform ( form )
    {
    if (form.ctsz.value == "Not Chosen") {
    alert( "Choose Size or choose opt out" );
    form.ctsz.focus();
    return false ;
    }
    return true ;
    }

    Any help appreciated.

    Garry Jones
    Sweden
     
    Garry Jones, Oct 5, 2006
    #1
    1. Advertisements

  2. Garry Jones

    McKirahan Guest

    "broken somewhere" is not very helpful to us.

    What does your form look like? Does it ihave:
    <form onsubmit="checkForm(this)" ...>
    and
    <select name="ctsz" ...>
    and
    <input type="submit" ...>

    Try using the Firefox browser to debug JavaScript.
    http://www.getfirefox.com
     
    McKirahan, Oct 5, 2006
    #2
    1. Advertisements

  3. Garry Jones

    RobG Guest

    That would need to be:

    <form onsubmit="return checkForm(this)" ...>
     
    RobG, Oct 5, 2006
    #3
  4. Garry Jones

    McKirahan Guest

    Good catch.
     
    McKirahan, Oct 5, 2006
    #4
  5. Garry Jones

    Garry Jones Guest

    "broken somewhere" is not very helpful to us.

    I have the checkform parameter and all values declared.

    I have been using a java code script to test for an empty field.

    This line of code results in the alert being triggered if the value is not
    entered

    if (form.deltport.value == "")

    I have only ever tested for empty fields and assumed thought I could adapt
    that code to test for a value.

    I want to test for the value of an option from a "select" control.

    Following the above example it should follow that this line of code

    if (form.ctsz.value == "Not Chosen")

    ..... triggers an alert if the value is Not Chosen - but it does not work-

    Greatful for any help in this matter.

    Garry Jones
    Sweden
     
    Garry Jones, Oct 6, 2006
    #5
  6. Garry Jones

    Garry Jones Guest

    Sorry, I have been stupid (again).

    I should not have tested for the text of the select control but the value
    submitted by the option.

    Now working, sorry to have wasted your time.

    Garry Jones
    Sweden
     
    Garry Jones, Oct 6, 2006
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.