for loop for unknown number of fields to validate

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

    for loop for unknown number of fields to validate

    I must confirm the user enters a value for each fund they need. I do not know
    how many fund entries there will be...it's expandable to handle each users
    needs. I must varify each fund they enter is six digits long. I have no
    problems with the Is_In_Format function (I found it on the web and am using
    it for other things).

    My problem is with my validate function which I've cut down (oh it doesn't
    work but you can see the direction I am going with it). Is there a better way
    to validate this? If not what am I doing wrong?


    function validate() {
    for(i=1;i<=nrow ;i++) {
    If (eval("!Is_In_F ormat( document.forms[0].fund_" + i + ".value, "dddddd")
    || document.forms[0].fund_" + i + ".value.len gth != 6")){
    alert("Fund must be 6 digits");
    eval("document. forms[0].fund_" + i + ".focus();" )
    }
    }
  • Mick White

    #2
    Re: for loop for unknown number of fields to validate

    Abby Lee wrote:
    [color=blue]
    > I must confirm the user enters a value for each fund they need. I do not know
    > how many fund entries there will be...it's expandable to handle each users
    > needs. I must varify each fund they enter is six digits long. I have no
    > problems with the Is_In_Format function (I found it on the web and am using
    > it for other things).
    >
    > My problem is with my validate function which I've cut down (oh it doesn't
    > work but you can see the direction I am going with it). Is there a better way
    > to validate this? If not what am I doing wrong?
    >
    >
    > function validate() {
    > for(i=1;i<=nrow ;i++) {
    > If (eval("!Is_In_F ormat( document.forms[0].fund_" + i + ".value, "dddddd")
    > || document.forms[0].fund_" + i + ".value.len gth != 6")){
    > alert("Fund must be 6 digits");
    > eval("document. forms[0].fund_" + i + ".focus();" )
    > }
    > }[/color]
    function validate(){
    f-document.forms[0];
    for(i=1;i<=nrow ;i++) {
    if(f["fund"+i].value){
    if(!/^\d{6}$/.test(f["fund"+i].value){
    alert("Fund must be 6 digits");
    f["fund_" + i].focus();
    return false;
    }
    }
    }
    return true;
    }

    I am assuming empty fields are OK.
    Mick

    Comment

    • Abby Lee

      #3
      Re: for loop for unknown number of fields to validate

      Mick White <mwhite13@roche ster.rr.com> writes:
      [color=blue]
      > Abby Lee wrote:
      >[color=green]
      > > I must confirm the user enters a value for each fund they need. I do not
      > > know
      > > how many fund entries there will be...it's expandable to handle each
      > > users
      > > needs. I must varify each fund they enter is six digits long. I have no
      > > problems with the Is_In_Format function (I found it on the web and am
      > > using
      > > it for other things).
      > >
      > > My problem is with my validate function which I've cut down (oh it
      > > doesn't
      > > work but you can see the direction I am going with it). Is there a better
      > > way
      > > to validate this? If not what am I doing wrong?
      > >
      > >
      > > function validate() {
      > > for(i=1;i<=nrow ;i++) {
      > > If (eval("!Is_In_F ormat( document.forms[0].fund_" + i + ".value,
      > > "dddddd")
      > > || document.forms[0].fund_" + i + ".value.len gth != 6")){
      > > alert("Fund must be 6 digits");
      > > eval("document. forms[0].fund_" + i + ".focus();" )
      > > }
      > > }[/color]
      > function validate(){
      > f-document.forms[0];
      > for(i=1;i<=nrow ;i++) {
      > if(f["fund"+i].value){
      > if(!/^\d{6}$/.test(f["fund"+i].value){
      > alert("Fund must be 6 digits");
      > f["fund_" + i].focus();
      > return false;
      > }
      > }
      > }
      > return true;
      > }
      >
      > I am assuming empty fields are OK.
      > Mick[/color]

      Mick,
      Can you explain what you are doing here...I could not get it to work, even
      after changing it to check just the one "fund" field they must use.

      function validate(){
      if(!/^\d{6}$/.test(document. forms[0].fund_1.value){
      alert("Fund must be 6 digits");
      document.forms[0].fund_1.focus() ;
      }
      else document.forms[0].submit();
      }

      Comment

      • Abby Lee

        #4
        Re: for loop for unknown number of fields to validate

        Abby Lee <abbylee26@hotm ail.com> writes:
        [color=blue]
        > Mick White <mwhite13@roche ster.rr.com> writes:
        >[color=green]
        > > Abby Lee wrote:
        > >[color=darkred]
        > > > I must confirm the user enters a value for each fund they need. I do
        > > > not
        > > > know
        > > > how many fund entries there will be...it's expandable to handle each
        > > > users
        > > > needs. I must varify each fund they enter is six digits long. I have no
        > > >
        > > > problems with the Is_In_Format function (I found it on the web and am
        > > > using
        > > > it for other things).
        > > >
        > > > My problem is with my validate function which I've cut down (oh it
        > > > doesn't
        > > > work but you can see the direction I am going with it). Is there a
        > > > better
        > > > way
        > > > to validate this? If not what am I doing wrong?
        > > >
        > > >
        > > > function validate() {
        > > > for(i=1;i<=nrow ;i++) {
        > > > If (eval("!Is_In_F ormat( document.forms[0].fund_" + i + ".value,
        > > > "dddddd")
        > > > || document.forms[0].fund_" + i + ".value.len gth != 6")){
        > > > alert("Fund must be 6 digits");
        > > > eval("document. forms[0].fund_" + i + ".focus();" )
        > > > }
        > > > }[/color]
        > > function validate(){
        > > f-document.forms[0];
        > > for(i=1;i<=nrow ;i++) {
        > > if(f["fund"+i].value){
        > > if(!/^\d{6}$/.test(f["fund"+i].value){
        > > alert("Fund must be 6 digits");
        > > f["fund_" + i].focus();
        > > return false;
        > > }
        > > }
        > > }
        > > return true;
        > > }
        > >
        > > I am assuming empty fields are OK.
        > > Mick[/color]
        >
        > Mick,
        > Can you explain what you are doing here...I could not get it to work, even
        > after changing it to check just the one "fund" field they must use.
        >
        > function validate(){
        > if(!/^\d{6}$/.test(document. forms[0].fund_1.value){
        > alert("Fund must be 6 digits");
        > document.forms[0].fund_1.focus() ;
        > }
        > else document.forms[0].submit();
        > }[/color]


        Nevermind and thank you Mick, I got it working ;)

        Comment

        • Dr John Stockton

          #5
          Re: for loop for unknown number of fields to validate

          JRS: In article <uiucguest.2004 0426155829$7cde @news.ks.uiuc.e du>, seen
          in news:comp.lang. javascript, Abby Lee <abbylee26@hotm ail.com> posted at
          Mon, 26 Apr 2004 11:08:04 :[color=blue]
          >I must confirm the user enters a value for each fund they need. I do not know
          >how many fund entries there will be...it's expandable to handle each users
          >needs. I must varify each fund they enter is six digits long. I have no
          >problems with the Is_In_Format function (I found it on the web and am using
          >it for other things).
          >
          >My problem is with my validate function which I've cut down (oh it doesn't
          >work but you can see the direction I am going with it). Is there a better way
          >to validate this? If not what am I doing wrong?
          >
          >
          >function validate() {
          > for(i=1;i<=nrow ;i++) {
          > If (eval("!Is_In_F ormat( document.forms[0].fund_" + i + ".value, "dddddd")
          >|| document.forms[0].fund_" + i + ".value.len gth != 6")){
          > alert("Fund must be 6 digits");
          > eval("document. forms[0].fund_" + i + ".focus();" )
          > }
          > }[/color]

          Rather than using some dubious function from the Web, why not test
          directly with a RegExp /$\d{6}$/ ?

          <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VFF> has a general
          method, shown in ...#TC, using an Array in an Object to define all the
          tests on a form; it should be possible to replace the scanning of the
          array with scanning what I suppose to be an array of elements.

          You want something like

          function validate() { var F, OK
          for(i=0;i<nrow; i++) { F = document.forms[0].element[i] // or similar
          OK = /$\d{6}$/.test(F.value) // 6 digits only
          if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
          return false } }
          return true }

          FAQ 4.40, 4.39, of course.

          --
          © 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> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Mick White

            #6
            Re: for loop for unknown number of fields to validate

            Dr John Stockton wrote:

            <snip>[color=blue]
            >
            > function validate() { var F, OK
            > for(i=0;i<nrow; i++) { F = document.forms[0].element[i] // or similar
            > OK = /$\d{6}$/.test(F.value) // 6 digits only
            > if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
            > return false } }
            > return true }
            >
            > FAQ 4.40, 4.39, of course.
            >[/color]
            OP states that empty fields are OK. But if there is an entry, it should
            be 6 digits.
            See my earlier posting.
            Mick

            Comment

            • Dr John Stockton

              #7
              Re: for loop for unknown number of fields to validate

              JRS: In article <zVgjc.120979$e 17.17168@twiste r.nyroc.rr.com> , seen in
              news:comp.lang. javascript, Mick White <mwhite13@roche ster.rr.com> posted
              at Mon, 26 Apr 2004 23:27:27 :[color=blue]
              >Dr John Stockton wrote:
              >
              ><snip>[color=green]
              >>
              >> function validate() { var F, OK
              >> for(i=0;i<nrow; i++) { F = document.forms[0].element[i] // or similar
              >> OK = /$\d{6}$/.test(F.value) // 6 digits only
              >> if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
              >> return false } }
              >> return true }
              >>
              >> FAQ 4.40, 4.39, of course.
              >>[/color]
              >OP states that empty fields are OK. But if there is an entry, it should
              >be 6 digits.[/color]

              (a) The first $ above should be ^
              (b) OK = /^$|^\d{6}$/.test( ) // seems OK; empty or 6-digit

              --
              © 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> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...