Form validation script problem - your help needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray@goodnewswebdesign.com

    #1

    Form validation script problem - your help needed

    Hi all,

    I'm new to JavaScript and am trying to create a client side JavaScript form
    validation script. I'm doing ok with validating text input boxes but have
    a problem that I have not been able to solve and one I hope someone will
    help me resolve.

    I want to check the contents of a drop down box and if a certain item is
    selected, check the contents of a text input box to see if a any text was
    entered. If no text is entered, I want to put up a notice so the user
    won't forget.

    Here is a code segment that does work:

    if (feedbackform.s ervices.value == 'none selected') {
    alert("Please provide us which service option you are interested
    in.");
    feedbackform.se rvices.focus();
    return false;
    }


    And here is the segment of code that to me looks ok, but it will not work
    as expected.

    if (feedbackform.s ervices.value == 'Existing website redesign' &&
    feedbackform.cu rrenturl.value. length == 0)
    {
    alert("Please provide us with a URL");
    feedbackform.cu rrenturl.focus( );
    return false;
    }


    Any help appreciated.

    Ray

  • RobG

    #2
    Re: Form validation script problem - your help needed

    Ray@goodnewsweb design.com wrote:[color=blue]
    > Hi all,
    >
    > I'm new to JavaScript and am trying to create a client side JavaScript form
    > validation script. I'm doing ok with validating text input boxes but have
    > a problem that I have not been able to solve and one I hope someone will
    > help me resolve.
    >
    > I want to check the contents of a drop down box and if a certain item is
    > selected, check the contents of a text input box to see if a any text was
    > entered. If no text is entered, I want to put up a notice so the user
    > won't forget.
    >
    > Here is a code segment that does work:
    >
    > if (feedbackform.s ervices.value == 'none selected') {
    > alert("Please provide us which service option you are interested
    > in.");
    > feedbackform.se rvices.focus();
    > return false;
    > }
    >
    >
    > And here is the segment of code that to me looks ok, but it will not work
    > as expected.
    >
    > if (feedbackform.s ervices.value == 'Existing website redesign' &&
    > feedbackform.cu rrenturl.value. length == 0)
    > {
    > alert("Please provide us with a URL");
    > feedbackform.cu rrenturl.focus( );
    > return false;
    > }
    >
    >
    > Any help appreciated.
    >
    > Ray
    >[/color]

    Given that your logic depends on what is coded in the HTML, we need
    to see the HTML related to your script. You have also left us guess
    as to what 'will not work as expected' is, nor which browser you are
    developing & testing in (sometimes it matters, sometimes not).

    I'll guess that the alert does not show up when you don't put any
    text into 'feedbackform.c urrenturl'. The script in that regard looks
    fine.

    The other part: feedbackform.se rvices.value - infers that you are
    trying to get the value of a select element. In that case, you may
    be depending on the value of the select being the text of the
    selected option because you haven't given it a value attribute -
    that is what the HTML spec says and is how things work in some
    browsers (e.g. Firefox) but not others (e.g. IE).

    But you have set 'Existing website redesign' as the value, or it's
    a text input and the value gets there some other way, ...

    Maybe you've mis-typed a form element name, or ...

    --
    Rob

    Comment

    • Dietmar Meier

      #3
      Re: Form validation script problem - your help needed

      Ray@goodnewsweb design.com wrote:
      [color=blue]
      > And here is the segment of code that to me looks ok, but it will not
      > work as expected.
      >
      > if (feedbackform.s ervices.value == 'Existing website redesign' &&
      > feedbackform.cu rrenturl.value. length == 0)
      > {
      > alert("Please provide us with a URL");
      > feedbackform.cu rrenturl.focus( );
      > return false;
      > }[/color]

      Assumed that "feedbackfo rm" is a valid reference to a form, try:

      var mySelect, myTextInput;
      if ((mySelect = feedbackform.el ements['services'])
      && mySelect.option s[mySelect.select edIndex].value
      == 'Existing website redesign'
      && (myTextInput = feedbackform.el ements['currenturl'])
      && !myTextInput.va lue
      ) {
      window.alert("P lease provide us with a URL");
      if (myTextInput.fo cus) {
      myTextInput.foc us();
      }
      return false;
      }

      ciao, dhgm

      Comment

      • RobB

        #4
        Re: Form validation script problem - your help needed

        R...@goodnewswe bdesign.com wrote:[color=blue]
        > Hi all,
        >
        > I'm new to JavaScript and am trying to create a client side[/color]
        JavaScript form[color=blue]
        > validation script. I'm doing ok with validating text input boxes but[/color]
        have[color=blue]
        > a problem that I have not been able to solve and one I hope someone[/color]
        will[color=blue]
        > help me resolve.
        >
        > I want to check the contents of a drop down box and if a certain item[/color]
        is[color=blue]
        > selected, check the contents of a text input box to see if a any text[/color]
        was[color=blue]
        > entered. If no text is entered, I want to put up a notice so the[/color]
        user[color=blue]
        > won't forget.
        >
        > Here is a code segment that does work:
        >
        > if (feedbackform.s ervices.value == 'none selected') {
        > alert("Please provide us which service option you are[/color]
        interested[color=blue]
        > in.");
        > feedbackform.se rvices.focus();
        > return false;
        > }
        >
        >
        > And here is the segment of code that to me looks ok, but it will not[/color]
        work[color=blue]
        > as expected.
        >
        > if (feedbackform.s ervices.value == 'Existing website redesign' &&
        > feedbackform.cu rrenturl.value. length == 0)
        > {
        > alert("Please provide us with a URL");
        > feedbackform.cu rrenturl.focus( );
        > return false;
        > }
        >
        >
        > Any help appreciated.
        >
        > Ray[/color]

        Just pilin' on here...

        function checkform(els)
        {
        if (els.services.s electedIndex == 3
        && /^\s*$/.test(els.curre nturl.value))
        {
        alert("Please provide us with a URL");
        if (els.currenturl .focus)
        els.currenturl. focus(­);
        return false;
        }
        return true;
        }

        <form name="feedbackf orm"...onsubmit ="return checkform(this. elements)">

        The selectedIndex of '3' refers to the fourth option (OK, you knew
        that). The argument against using it instead of the option value is:
        additions to the form may render it incorrect. For a simple application
        like this, however, the simplicity is appealing.

        Comment

        • RobG

          #5
          Re: Form validation script problem - your help needed

          RobB wrote:
          [...][color=blue]
          > Just pilin' on here...
          >
          > function checkform(els)
          > {
          > if (els.services.s electedIndex == 3
          > && /^\s*$/.test(els.curre nturl.value))
          > {
          > alert("Please provide us with a URL");
          > if (els.currenturl .focus)
          > els.currenturl. focus(­);
          > return false;
          > }
          > return true;[/color]

          'Stacks on the mill' I believe it's called...

          Maybe we can crystal-ball one step further and guess that the OP also
          requires a URL validation test - simply testing for something or
          nothing isn't exactly rigorous... :-x

          I'll kick it off... (I've kept the invented '3')

          if (els.services.s electedIndex == 3
          && /^http[s]?:\/\/\w+/.test(els.curre nturl.value))
          {

          Is likely as tough as it should get. Test any tighter and things
          like anchors or query strings get rejected (or maybe they should?).


          [...]


          --
          Rob

          Comment

          • ray@goodnewswebdesign.com

            #6
            Re: Form validation script problem - your help needed

            On Fri, 15 Apr 2005 01:28:26 +0200, "Dietmar Meier"
            <usereplytoinst ead@innoline-systemtechnik.d e> wrote:
            [color=blue]
            >Ray@goodnewswe bdesign.com wrote:
            >[color=green]
            >> And here is the segment of code that to me looks ok, but it will not
            >> work as expected.
            >>
            >> if (feedbackform.s ervices.value == 'Existing website redesign' &&
            >> feedbackform.cu rrenturl.value. length == 0)
            >> {
            >> alert("Please provide us with a URL");
            >> feedbackform.cu rrenturl.focus( );
            >> return false;
            >> }[/color]
            >
            >Assumed that "feedbackfo rm" is a valid reference to a form, try:
            >
            >var mySelect, myTextInput;
            >if ((mySelect = feedbackform.el ements['services'])
            > && mySelect.option s[mySelect.select edIndex].value
            > == 'Existing website redesign'
            > && (myTextInput = feedbackform.el ements['currenturl'])
            > && !myTextInput.va lue
            >) {
            > window.alert("P lease provide us with a URL");
            > if (myTextInput.fo cus) {
            > myTextInput.foc us();
            > }
            > return false;
            >}
            >
            >ciao, dhgm[/color]

            Hi Dietmar and RobG and RobB,

            Thanks for replying!

            Dietmar, I tried inserting your code, but it did not work. The form itself
            works and I do receive feedback through it. However, I decided to add a
            little bit of client side checking because of the number of forms I get
            with one field or another left blank accidently. I am testing the
            scripts on IE 5.5 and on Firefox 1.0.2 and the page behaves the same on
            both.

            If I didnt' make myself clear before, what I'm trying to achieve is that if
            the user selects website redesign from a drop down menu selector, I want to
            check (at submit time) whether he has entered a URL of his current site.

            The page I'm working on can be found here:




            Raymond Cantillon
            (Owner - Good News Web Design)
            --
            ------------------------------------------------------------------------
            Put your church or other christian based organization
            on the internet today! Visit our website:

            A Christian Web Design Company
            ------------------------------------------------------------------------

            Comment

            • Dietmar Meier

              #7
              Re: Form validation script problem - your help needed

              ray@goodnewsweb design.com wrote:
              [color=blue][color=green]
              >> && mySelect.option s[mySelect.select edIndex].value
              >> == 'Existing website redesign'[/color][/color]
              [color=blue]
              > Dietmar, I tried inserting your code, but it did not work.[/color]

              Maybe that's because you simply copied & pasted it?

              "Existing website redesign" is a value of the text property, not
              the value property, of one of your select's options. So either
              replace ".value" with ".text", or replace "== 'Existing website
              redesign'" with "== '4'".

              ciao, dhgm

              Comment

              • Ray@goodnewswebdesign.com

                #8
                Re: Form validation script problem - your help needed

                On Mon, 18 Apr 2005 15:57:22 +0200, "Dietmar Meier"
                <usereplytoinst ead@innoline-systemtechnik.d e> wrote:
                [color=blue]
                >ray@goodnewswe bdesign.com wrote:
                >[color=green][color=darkred]
                >>> && mySelect.option s[mySelect.select edIndex].value
                >>> == 'Existing website redesign'[/color][/color]
                >[color=green]
                >> Dietmar, I tried inserting your code, but it did not work.[/color]
                >
                >Maybe that's because you simply copied & pasted it?
                >[/color]

                This is true. I changed it as you suggested below and it now works
                beautifully! Thank you for your kind assistance!

                Ray
                [color=blue]
                >"Existing website redesign" is a value of the text property, not
                >the value property, of one of your select's options. So either
                >replace ".value" with ".text", or replace "== 'Existing website
                >redesign'" with "== '4'".
                >
                >ciao, dhgm[/color]

                --
                ------------------------------------------------------------------------
                Put your church or other christian based organization
                on the internet today! Visit our website:

                A Christian Web Design Company
                King James Bible - kingjamesbible. goodnewswebdesi gn.com
                Catholic Catechism - catholiccatechi sm.goodnewswebd esign.com
                ------------------------------------------------------------------------

                Comment

                Working...