Validating a drop down (select) type, form element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Murray

    Validating a drop down (select) type, form element

    Hi everybody:


    I am trying to write a generic function, isValidMonth, that can used on 2
    form elements, smonth and emonth. I would like the function to test whether
    the user has selected a month or not.
    If anyone can help me I would really appreciate it. What I have done
    follows.

    My form has:

    <SELECT name=smonth>
    <OPTION value=0>-Month-
    <OPTION value=1>Jan
    <OPTION value=2>Feb
    <OPTION value=3>Mar
    ...
    <OPTION value=12>Dec
    </SELECT>

    <SELECT name=emonth>
    <OPTION value=0>-Month-
    <OPTION value=1>Jan
    <OPTION value=2>Feb
    <OPTION value=3>Mar
    ...
    <OPTION value=12>Dec
    </SELECT>
    ..
    My JavaScript contains:

    function isValidMonth(mo nth) {
    if (month[month.selectedI ndex].value == 0
    return false
    return true
    }


    function submitIt(form) {
    if (!isValidmonth( smonth))
    alert("You have not selected a start month")
    return true
    }

    Thank you

    Terry


  • kaeli

    #2
    Re: Validating a drop down (select) type, form element

    In article <Q6jLb.34347$AJ B.14159@news04. bloor.is.net.ca ble.rogers.com> ,
    tgmurray@rogers .com enlightened us with...[color=blue]
    >
    > <SELECT name=smonth>[/color]

    quote attributes.
    sname="month"
    [color=blue]
    > <OPTION value=0>-Month-[/color]
    value="0"
    [color=blue]
    > .
    > My JavaScript contains:
    >
    > function isValidMonth(mo nth) {
    > if (month[month.selectedI ndex].value == 0
    > return false
    > return true
    > }
    >[/color]

    Better programming practice (only one exit) and a correction

    function isValidMonth(mo nth) {
    b = true;
    if (month.options[month.selectedI ndex].value == 0
    b = false
    return b
    }
    [color=blue]
    >
    > function submitIt(form) {
    > if (!isValidmonth( smonth))
    > alert("You have not selected a start month")
    > return true
    > }
    >[/color]

    return value needed; keyword changed; correction to call

    function submitIt(frm) {
    b = true;
    if (isValidmonth(f rm.smonth))
    {
    alert("You have not selected a start month");
    b = false;
    }
    return b
    }

    handler called in onSubmit of form, not onClick of submit

    <form name="f1" action="whateve r" method="post" onSubmit="retur n
    submitIt(this)" >

    --
    --
    ~kaeli~
    A little rudeness and disrespect can elevate a meaningless
    interaction to a battle of wills and add drama to an
    otherwise dull day.



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Validating a drop down (select) type, form element

      kaeli <tiny_one@NOSPA M.comcast.net> writes:
      [color=blue]
      > Better programming practice (only one exit) and a correction[/color]

      I'm not sure I agree with that practice in all cases.
      [color=blue]
      > function isValidMonth(mo nth) {
      > b = true;
      > if (month.options[month.selectedI ndex].value == 0[/color]

      Missing ")". I recommend { ... } around all if branches, even if they
      are only a single statement. It makes it easier to extend them later.

      From the original poster's description, I would just test:
      month.selectedI ndex == 0
      since his invalid option was the first one.
      [color=blue]
      > b = false
      > return b
      > }[/color]

      .... but when it comes to returning booleans with if-statements,
      I always want to do it shorter:

      function isValidMonth(mo nth) {
      return month.selectedI ndex != 0;
      }

      Otherwise, I agree.
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Terry Murray

        #4
        Re: Validating a drop down (select) type, form element

        Thanks Kaeli. I think a correction is needed for the code that you provided
        me with.
        if (!isValidmonth( frm.smonth)) should replace if
        (isValidmonth(f rm.smonth)) inside of function submitIt(frm).

        Terry



        "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message
        news:MPG.1a6784 3e98fd5a32989a9 a@nntp.lucent.c om...[color=blue]
        > In article <Q6jLb.34347$AJ B.14159@news04. bloor.is.net.ca ble.rogers.com> ,
        > tgmurray@rogers .com enlightened us with...[color=green]
        > >
        > > <SELECT name=smonth>[/color]
        >
        > quote attributes.
        > sname="month"
        >[color=green]
        > > <OPTION value=0>-Month-[/color]
        > value="0"
        >[color=green]
        > > .
        > > My JavaScript contains:
        > >
        > > function isValidMonth(mo nth) {
        > > if (month[month.selectedI ndex].value == 0
        > > return false
        > > return true
        > > }
        > >[/color]
        >
        > Better programming practice (only one exit) and a correction
        >
        > function isValidMonth(mo nth) {
        > b = true;
        > if (month.options[month.selectedI ndex].value == 0
        > b = false
        > return b
        > }
        >[color=green]
        > >
        > > function submitIt(form) {
        > > if (!isValidmonth( smonth))
        > > alert("You have not selected a start month")
        > > return true
        > > }
        > >[/color]
        >
        > return value needed; keyword changed; correction to call
        >
        > function submitIt(frm) {
        > b = true;
        > if (isValidmonth(f rm.smonth))
        > {
        > alert("You have not selected a start month");
        > b = false;
        > }
        > return b
        > }
        >
        > handler called in onSubmit of form, not onClick of submit
        >
        > <form name="f1" action="whateve r" method="post" onSubmit="retur n
        > submitIt(this)" >
        >
        > --
        > --
        > ~kaeli~
        > A little rudeness and disrespect can elevate a meaningless
        > interaction to a battle of wills and add drama to an
        > otherwise dull day.
        > http://www.ipwebdesign.net/wildAtHeart
        > http://www.ipwebdesign.net/kaelisSpace
        >[/color]


        Comment

        Working...