submiting after for loop in page validations

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

    submiting after for loop in page validations

    I have a for statement in my validation process
    I do a for loop becasue I don't know how many rows of items there are...I
    need to validate each item in each row.
    If I remove the "document.f orms[0].submit(); it works perfectly...the code
    stops at each field that is not formated properly...but never submits.
    However if I leave the submit() in there, the page submits before they have a
    chance to fix problems.

    How can I get both to work properly?


    function validate(){
    for(i=1;i<=rows ;i++){
    if(column 1 is formated right){
    alert("message" );
    here.focus();
    }
    if (column 2 is formated right){
    alert("message" );
    here.focus();
    }

    document.forms[0].submit();
    }
  • Mick White

    #2
    Re: submiting after for loop in page validations

    Abby Lee wrote:
    [color=blue]
    > I have a for statement in my validation process
    > I do a for loop becasue I don't know how many rows of items there are...I
    > need to validate each item in each row.
    > If I remove the "document.f orms[0].submit(); it works perfectly...the code
    > stops at each field that is not formated properly...but never submits.
    > However if I leave the submit() in there, the page submits before they have a
    > chance to fix problems.
    >
    > How can I get both to work properly?
    >
    >
    > function validate(){
    > for(i=1;i<=rows ;i++){
    > if(column 1 is formated right){
    > alert("message" );
    > here.focus();
    > }
    > if (column 2 is formated right){
    > alert("message" );
    > here.focus();
    > }
    >
    > document.forms[0].submit();
    > }[/color]
    This will always submit the form, include a "return false" after an
    error, or better yet rewrite the function so that it returns a boolean
    value.

    <form onsubmit="retur n validate()">
    Mick

    Comment

    • Richard Cornford

      #3
      Re: submiting after for loop in page validations

      Abby Lee wrote:[color=blue]
      > I have a for statement in my validation process
      > I do a for loop becasue I don't know how many rows of items there
      > are...I need to validate each item in each row.
      > If I remove the "document.f orms[0].submit(); it works perfectly...the
      > code stops at each field that is not formated properly...but never
      > submits. However if I leave the submit() in there, the page submits
      > before they have a chance to fix problems.
      >
      > How can I get both to work properly?
      >
      >
      > function validate(){
      > for(i=1;i<=rows ;i++){
      > if(column 1 is formated right){
      > alert("message" );
      > here.focus();
      > }
      > if (column 2 is formated right){
      > alert("message" );
      > here.focus();
      > }
      >
      > document.forms[0].submit();
      > }[/color]

      This idea you have that javascript that is supposed to interact with
      HTML can be considered in isolation form the HTML it is interacting with
      is misguided.

      Normal form validation is done by putting an - onsubmit - attribute in
      the opening form tag that calls the validation function and returns
      whatever value that function returns:-

      <form name="x" action="http://example.com/exPage.jsp"
      onsubmit="retur n validateThisFor m(this);">

      - The example - validateThisFor m - function would then return - false -
      if the form did not validate (cancelling the submission of the form and
      giving the user a chance to correct it), or it would return - true - and
      allow the form to be submitted.

      Your function returns neither true or false, and you have not provided
      any information on the context in which it is used. But, generally,
      directly calling the - submit - method of a form is a bad idea that
      results in a client-side scripting dependency that is avoidable with the
      normal use of HTML forms and offers no benefits in return for that
      needless dependency.

      Richard.


      Comment

      • Abby Lee

        #4
        Re: submiting after for loop in page validations

        Mick White <mwhite13@BOGUS rochester.rr.co m> writes:
        [color=blue]
        > Abby Lee wrote:
        >[color=green]
        > > I have a for statement in my validation process
        > > I do a for loop becasue I don't know how many rows of items there are...I
        > >
        > > need to validate each item in each row.
        > > If I remove the "document.f orms[0].submit(); it works perfectly...the
        > > code
        > > stops at each field that is not formated properly...but never submits.
        > > However if I leave the submit() in there, the page submits before they
        > > have a
        > > chance to fix problems.
        > >
        > > How can I get both to work properly?
        > >
        > >
        > > function validate(){
        > > for(i=1;i<=rows ;i++){
        > > if(column 1 is formated right){
        > > alert("message" );
        > > here.focus();
        > > }
        > > if (column 2 is formated right){
        > > alert("message" );
        > > here.focus();
        > > }
        > >
        > > document.forms[0].submit();
        > > }[/color]
        > This will always submit the form, include a "return false" after an
        > error, or better yet rewrite the function so that it returns a boolean
        > value.
        >
        > <form onsubmit="retur n validate()">
        > Mick[/color]
        My code just submits without doing any validation. Understand I'm new to
        this..can you be of more help?

        function validate(){
        for(i=1;i<=rows ;i++){
        if(column 1 is formated right){
        alert("message" );
        here.focus();
        return false;
        }
        if (column 2 is formated right){
        alert("message" );
        here.focus();
        return false;
        }
        }

        --------<sbip>---------
        <input type="submit" name="Submit" value="Submit Changes" onSubmit="retur n
        validate()">

        Comment

        • Mick White

          #5
          Re: submiting after for loop in page validations

          Abby Lee wrote:
          [color=blue]
          > My code just submits without doing any validation. Understand I'm new to
          > this..can you be of more help?
          >
          > function validate(){
          > for(i=1;i<=rows ;i++){
          > if(column 1 is formated right){
          > alert("message" );
          > here.focus();
          > return false;
          > }
          > if (column 2 is formated right){
          > alert("message" );
          > here.focus();
          > return false;
          > }
          > }
          >[/color]

          I am not sure if you're trying to validate form elements, it seems not.
          First, I would name the function something meaningful.


          function validateColumnF ormat(){
          for(i=1;i<=rows ;i++){
          if(column 1 is formatted wrong){
          alert("message" );
          here.focus();
          return false;
          }
          if (column 2 is formatted wrong){
          alert("message" );
          here.focus();
          return false;
          }
          }
          return true;
          }

          Then you may test the condition:

          if(validateColu mnFormat()){ //do stuff }

          Or set a boolean , dependant on the condition:
          var ready=validateC olumnFormat();
          Or a variable
          var foobar=validate ColumnFormat()? "white":"gr ey";

          Mick



          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: submiting after for loop in page validations

            Abby Lee wrote:
            [color=blue]
            > I have a for statement in my validation process
            > I do a for loop becasue I don't know how many rows of items there are...I
            > need to validate each item in each row.
            > [...]
            > function validate(){
            > for(i=1;i<=rows ;i++){[/color]

            Non sequitur. A "for" loop is determined and as such the exact opposite
            of what you want to do if you do not know the number of rows, unless your
            evil[tm] global "rows" determines the number of rows previously (if so,
            then why don't you determine it within the method?).


            PointedEars

            Comment

            • Shawn Milo

              #7
              Re: submiting after for loop in page validations

              You can just use an ordinary submit button -- take that
              submit() line out of the code. Then, add the following
              to the <form> declaration:

              onsubmit="retur n validateForm(); "

              Then, have your function keep a boolean (true/false),
              declared true at the top, and make it false if the
              validation fails at any point. Return that value
              at the end of the function.

              Example:

              function validateAddPage (){

              var allOkay = true;
              var errMsg = '';

              if (document.forms['frmAdd'].txtTitle.value == ''){
              errMsg = errMsg + 'A project title must be entered.\n';
              var allOkay = false;
              }

              if (document.forms['frmAdd'].drpRelevance.s electedIndex == 0){
              errMsg = errMsg + 'A strategic relevance must be entered.\n';
              var allOkay = false;
              }

              if (document.forms['frmAdd'].drpCompany.sel ectedIndex == 0){
              errMsg = errMsg + 'A company must be entered.\n';
              var allOkay = false;
              }

              if (document.forms['frmAdd'].txtCompletion. value == ''){
              errMsg = errMsg + 'A requested completion date must be entered.\n';
              var allOkay = false;
              }



              if (!allOkay){aler t(errMsg);}

              return allOkay;

              }

              Comment

              Working...