Form validation problem...

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

    Form validation problem...

    I have a form that's used to sort a series of items. The form has a number
    of text fields. Each text field should contain a number. When the form is
    submitted I would like to do two things:

    1. Verify that only numbers are entered in each text field.
    2. Verify that there are no duplicate entries in any text field.

    So each field should be a unique number. Another thing complicating this is
    that I do not know how many fields I will have on any given form. I am
    building this form dynamically from a database call. So the function would
    need to parse through the form and check each field, without specifying the
    name of the field...

    I realize this may be simple for most of you (I'm hoping...), but I'm WAY
    rusty on my Javascript, having spent the past few years doing mostly
    server-side coding...

    THANKS for your help!!!

    E.


  • Douglas Crockford

    #2
    Re: Form validation problem...

    > I have a form that's used to sort a series of items. The form has a number[color=blue]
    > of text fields. Each text field should contain a number. When the form is
    > submitted I would like to do two things:
    >
    > 1. Verify that only numbers are entered in each text field.
    > 2. Verify that there are no duplicate entries in any text field.
    >
    > So each field should be a unique number. Another thing complicating this is
    > that I do not know how many fields I will have on any given form. I am
    > building this form dynamically from a database call. So the function would
    > need to parse through the form and check each field, without specifying the
    > name of the field...[/color]

    What class is this for, Eddie? I hope you are taking it pass/fail.

    Comment

    • Eddie

      #3
      Re: Form validation problem...

      This isn't for a class. I'm building a small content management system for
      my company's intranet and need to create a form that sorts child nodes.
      I've been doing server-side coding (ASP,PHP,JSP) for a few years, but
      haven't done any JS coding in a LONG time. So please excuse my ignorance...
      Can you help?

      eddie

      "Douglas Crockford" <nospam@laserli nk.net> wrote in message
      news:bjafur$k7g $1@sun-news.laserlink. net...[color=blue][color=green]
      > > I have a form that's used to sort a series of items. The form has a[/color][/color]
      number[color=blue][color=green]
      > > of text fields. Each text field should contain a number. When the form[/color][/color]
      is[color=blue][color=green]
      > > submitted I would like to do two things:
      > >
      > > 1. Verify that only numbers are entered in each text field.
      > > 2. Verify that there are no duplicate entries in any text field.
      > >
      > > So each field should be a unique number. Another thing complicating[/color][/color]
      this is[color=blue][color=green]
      > > that I do not know how many fields I will have on any given form. I am
      > > building this form dynamically from a database call. So the function[/color][/color]
      would[color=blue][color=green]
      > > need to parse through the form and check each field, without specifying[/color][/color]
      the[color=blue][color=green]
      > > name of the field...[/color]
      >
      > What class is this for, Eddie? I hope you are taking it pass/fail.
      >[/color]


      Comment

      • kaeli

        #4
        Re: Form validation problem...

        In article <20030905130522 .213$Vg@news.ne wsreader.com>, eddie@msn.com
        enlightened us with...[color=blue]
        > I have a form that's used to sort a series of items. The form has a number
        > of text fields. Each text field should contain a number.[/color]

        parseInt
        [color=blue]
        > When the form is
        > submitted I would like to do two things:
        >
        > 1. Verify that only numbers are entered in each text field.[/color]

        ..length
        [color=blue]
        > 2. Verify that there are no duplicate entries in any text field.[/color]

        put in array and look for matches
        [color=blue]
        > So each field should be a unique number. Another thing complicating this is
        > that I do not know how many fields I will have on any given form.[/color]

        loop through form.elements[] array


        -------------------------------------------------
        ~kaeli~
        Press any key to continue or any other key to quit.
        Who is General Failure and why is he reading
        my hard disk?


        -------------------------------------------------

        Comment

        • Eddie

          #5
          Re: Form validation problem...

          Thank you!!

          -eddie

          "kaeli" <infinite.possi bilities@NOSPAM att.net> wrote in message
          news:MPG.19c283 3d88e7da5f98982 e@nntp.lucent.c om...[color=blue]
          > In article <20030905130522 .213$Vg@news.ne wsreader.com>, eddie@msn.com
          > enlightened us with...[color=green]
          > > I have a form that's used to sort a series of items. The form has a[/color][/color]
          number[color=blue][color=green]
          > > of text fields. Each text field should contain a number.[/color]
          >
          > parseInt
          >[color=green]
          > > When the form is
          > > submitted I would like to do two things:
          > >
          > > 1. Verify that only numbers are entered in each text field.[/color]
          >
          > .length
          >[color=green]
          > > 2. Verify that there are no duplicate entries in any text field.[/color]
          >
          > put in array and look for matches
          >[color=green]
          > > So each field should be a unique number. Another thing complicating[/color][/color]
          this is[color=blue][color=green]
          > > that I do not know how many fields I will have on any given form.[/color]
          >
          > loop through form.elements[] array
          >
          >
          > -------------------------------------------------
          > ~kaeli~
          > Press any key to continue or any other key to quit.
          > Who is General Failure and why is he reading
          > my hard disk?
          > http://www.ipwebdesign.net/wildAtHeart
          > http://www.ipwebdesign.net/kaelisSpace
          > -------------------------------------------------[/color]


          Comment

          • kaeli

            #6
            Re: Form validation problem...

            In article <20030905133856 .190$v5@news.ne wsreader.com>, eddie@msn.com
            enlightened us with...[color=blue]
            > Thank you!!
            >[/color]

            You're welcome.
            And since you say it isn't for a class...
            [color=blue][color=green][color=darkred]
            > > > I have a form that's used to sort a series of items. The form has a[/color][/color]
            > number[color=green][color=darkred]
            > > > of text fields. Each text field should contain a number.[/color]
            > >[/color][/color]

            Here's my validation function.
            function checkNumeric(st rObject)
            {
            /* Returns true if the field has all numeric characters, false if
            not.
            You must pass in a input (text) object, not the value. */
            var index;
            var myChar;
            /* add chars to the following string if you want punctuation or
            spaces allowed */
            var allowedChars = "0123456789 "

            if (! strObject)
            {
            return false;
            }
            else
            {
            var str = strObject.value ;
            }

            for (index = 0; index < str.length; index ++)
            {
            myChar = str.charAt(inde x);
            if (allowedChars.i ndexOf(myChar) == -1)
            {
            return false;
            }
            }
            return true;
            }
            [color=blue][color=green]
            > >
            > > loop through form.elements[] array[/color][/color]

            x = document.formNa me.elements.len gth;
            for (i=0; i<x; i++)
            {
            e = document.formNa me.elements[i];
            if (e.type == "text")
            {
            // do something
            }
            }
            -------------------------------------------------
            ~kaeli~
            Press any key to continue or any other key to quit.
            Who is General Failure and why is he reading
            my hard disk?


            -------------------------------------------------

            Comment

            • Dr John Stockton

              #7
              Re: Form validation problem...

              JRS: In article <20030905130522 .213$Vg@news.ne wsreader.com>, seen in
              news:comp.lang. javascript, Eddie <eddie@msn.co m> posted at Fri, 5 Sep
              2003 10:05:23 :-[color=blue]
              >I have a form that's used to sort a series of items. The form has a number
              >of text fields. Each text field should contain a number. When the form is
              >submitted I would like to do two things:
              >
              >1. Verify that only numbers are entered in each text field.[/color]

              Firstly you must define "number". I presume that you do not mean
              "decimal digit". Can it be signed? Can it have a decimal point, dot or
              comma? Can it have thousands separators, comma or dot? Can it have an
              exponent, as in 3e5? Must it be integer?

              If, as seems probable, it must be a decimal digit string, then check it
              with either of the RegExps /^\d+$/ or /\D/ - though the latter accepts
              an empty string.
              [color=blue]
              >2. Verify that there are no duplicate entries in any text field.
              >
              >So each field should be a unique number. Another thing complicating this is
              >that I do not know how many fields I will have on any given form.[/color]

              Assuming that the number of fields is not ludicrously large :

              (1) Do var Them = [] // possibly superfluous
              (2) Do var T, U // subtle
              (3) For each field found, then do
              T = field.value // or +field.value
              if (Them[T]!=U) ... reject, otherwise
              Them[T] = 0

              Possibly != should be !==; test or think.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Form validation problem...

                Dr John Stockton <spam@merlyn.de mon.co.uk> writes:

                I agree except for details of style.
                [color=blue]
                > (1) Do var Them = [] // possibly superfluous[/color]

                You don't use that Them is an array, so you might as well just use an
                object (purely to satisfy the K.I.S.S. principle), i.e.,
                var Them = {};
                or
                var Them = new Object(); // for older browsers
                [color=blue]
                > (2) Do var T, U // subtle[/color]

                You use this to make U be the undefined value, most likely for compatability
                with old browsers.

                I would much rather set the global variable "undefined" once an for all:
                window.undefine d = window.undefine d;
                This is safe if undefined is already defined, and defines it if not.
                [color=blue]
                > (3) For each field found, then do
                > T = field.value // or +field.value
                > if (Them[T]!=U) ... reject, otherwise
                > Them[T] = 0
                >
                > Possibly != should be !==; test or think.[/color]

                Change to "Them[T] = true;" and you won't even have to think :)

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Richard Hockey

                  #9
                  Re: Form validation problem...


                  "Eddie" <eddie@msn.co m> wrote in message
                  news:2003090513 0522.213$Vg@new s.newsreader.co m...[color=blue]
                  > I have a form that's used to sort a series of items. The form has a[/color]
                  number[color=blue]
                  > of text fields. Each text field should contain a number. When the form[/color]
                  is[color=blue]
                  > submitted I would like to do two things:
                  >
                  > 1. Verify that only numbers are entered in each text field.[/color]

                  Try regular expressions:

                  <script type="text/javascript">
                  <!--
                  // set up regular expression to accept only numeric expressions made from
                  the digits 0 - 9
                  numericRE=/^[0-9]+&/;

                  function NumericValidate (fieldObject)
                  {
                  // test to see if contents of field match regular expression using
                  RE.test(string) method
                  if(!numericRE.t est(fieldObject .value))
                  {
                  alert('you have entered non-numeric characters in a numeric field. Please
                  enter numbers only in this field.');
                  fieldObject.foc us();
                  return false;
                  }

                  // field is numeric
                  return true;
                  }
                  -->
                  </script>


                  You could call this function from within another function when validating
                  the entire form before submitting, or on a 'onblur' event in the field
                  itself

                  <input type="text" name="accesscod e" onblur="Numeric Validate(this); ">
                  [color=blue]
                  > 2. Verify that there are no duplicate entries in any text field.
                  >
                  > So each field should be a unique number. Another thing complicating this[/color]
                  is[color=blue]
                  > that I do not know how many fields I will have on any given form. I am
                  > building this form dynamically from a database call. So the function[/color]
                  would[color=blue]
                  > need to parse through the form and check each field, without specifying[/color]
                  the[color=blue]
                  > name of the field...
                  >
                  > I realize this may be simple for most of you (I'm hoping...), but I'm WAY
                  > rusty on my Javascript, having spent the past few years doing mostly
                  > server-side coding...
                  >
                  > THANKS for your help!!!
                  >
                  > E.
                  >
                  >[/color]


                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: Form validation problem...

                    "Richard Hockey" <richardhockey@ dsl.pipex.com> writes:
                    [color=blue]
                    > Try regular expressions:
                    >
                    > <script type="text/javascript">
                    > <!--[/color]

                    HTML comments are not needed in Javascript.
                    [color=blue]
                    > // set up regular expression to accept only numeric expressions made from
                    > the digits 0 - 9
                    > numericRE=/^[0-9]+&/;[/color]

                    I think the "&" should be a "$".

                    For brevity, "[0-9]" can be replaced by "\d".

                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    Working...