checking and validating script

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

    #1

    checking and validating script

    Say I have 4 forms, all four have different numbers of text boxes. is there
    a script that I can use to check to make sure everything on the form is not
    blank?


  • Mick White

    #2
    Re: checking and validating script

    Samir wrote:
    [color=blue]
    > Say I have 4 forms, all four have different numbers of text boxes. is there
    > a script that I can use to check to make sure everything on the form is not
    > blank?
    >
    >[/color]

    function verifyNoBlankEn tries(){

    //Loop through forms:
    var f= document.forms. length;
    while(f--){

    // loop through each form control:
    fc=document.for ms[f].length;
    for(k=0;k<fc;k+ +){

    // look for empty text boxes
    if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
    alert("empty field");documen t.forms[f][k].focus();return false;
    }
    }
    }
    return true;
    }

    Mick

    Comment

    • Samir

      #3
      Re: checking and validating script

      Thanks, it works perfect.

      One more thing, is it possible to add a description to a textbox so that
      the warning can tell them what field it is?? does this also work for drop
      downs as well right?

      I know I am being picky.


      "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
      news:Xi_Gc.1695 6$bp1.5928@twis ter.nyroc.rr.co m...[color=blue]
      > Samir wrote:
      >[color=green]
      > > Say I have 4 forms, all four have different numbers of text boxes. is[/color][/color]
      there[color=blue][color=green]
      > > a script that I can use to check to make sure everything on the form is[/color][/color]
      not[color=blue][color=green]
      > > blank?
      > >
      > >[/color]
      >
      > function verifyNoBlankEn tries(){
      >
      > //Loop through forms:
      > var f= document.forms. length;
      > while(f--){
      >
      > // loop through each form control:
      > fc=document.for ms[f].length;
      > for(k=0;k<fc;k+ +){
      >
      > // look for empty text boxes
      > if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
      > alert("empty field");documen t.forms[f][k].focus();return false;
      > }
      > }
      > }
      > return true;
      > }
      >
      > Mick
      >[/color]


      Comment

      • Mick White

        #4
        Re: checking and validating script

        Samir wrote:
        [color=blue]
        > Thanks, it works perfect.
        >
        > One more thing, is it possible to add a description to a textbox so that
        > the warning can tell them what field it is??[/color]

        Yes it's possible (in most browsers), you could,for example, change the
        empty field's background to shocking pink if you like. Where would you
        like the description/warning to appear?


        does this also work for drop[color=blue]
        > downs as well right?[/color]


        No, but you can make sure that something is selected, or more precisely,
        if the selection has a value:

        if(document.for ms[f][k].type=="select-one" && !document.forms[f][k].value){
        alert("Select something from the "+document.form s[f][k].name+" menu");
        return false;
        }

        Ur if you use the first slot in all of the menus for something like
        "Select a ... ", you could rewrite the preceding code:

        if(document.for ms[f][k].type=="select-one" &&
        !document.forms[f][k].selectedIndex) {alert( ...}


        See if you can incorporate the code above into your page, let me know if
        you get stuck.

        Do all 4 of these forms get submitted at some point? I'm curious.

        Mick

        Original function below.

        function verifyNoBlankEn tries(){

        //Loop through forms:
        var f= document.forms. length;
        while(f--){

        // loop through each form control:
        fc=document.for ms[f].length;
        for(k=0;k<fc;k+ +){

        // look for empty text boxes
        if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
        alert("empty field");documen t.forms[f][k].focus();return false;
        }
        }
        }
        return true;
        }



        Comment

        • Samir

          #5
          Re: checking and validating script

          Okay for example you have a textbox
          <input type="text" name="T1" size="20">
          Say...is there a way to add something like
          <input type="text" name="T1" size="20" description="Fi rst Name">
          So when they leave it blank, the form validation check would do an alert of
          "You have left 'Then right here you would put the description of the
          textbox'"
          would something like that be possible? Kinda like you did for alert("Select
          something from the "+document.form s[f][k].name+" menu");

          but is a description or some kind of field that could give a better
          description? Same question goes for the drop down as well.

          Thanks again!

          "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
          news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...[color=blue]
          > Samir wrote:
          >[color=green]
          > > Thanks, it works perfect.
          > >
          > > One more thing, is it possible to add a description to a textbox so[/color][/color]
          that[color=blue][color=green]
          > > the warning can tell them what field it is??[/color]
          >
          > Yes it's possible (in most browsers), you could,for example, change the
          > empty field's background to shocking pink if you like. Where would you
          > like the description/warning to appear?
          >
          >
          > does this also work for drop[color=green]
          > > downs as well right?[/color]
          >
          >
          > No, but you can make sure that something is selected, or more precisely,
          > if the selection has a value:
          >
          > if(document.for ms[f][k].type=="select-one" &&[/color]
          !document.forms[f][k].value){[color=blue]
          > alert("Select something from the "+document.form s[f][k].name+" menu");
          > return false;
          > }
          >
          > Ur if you use the first slot in all of the menus for something like
          > "Select a ... ", you could rewrite the preceding code:
          >
          > if(document.for ms[f][k].type=="select-one" &&
          > !document.forms[f][k].selectedIndex) {alert( ...}
          >
          >
          > See if you can incorporate the code above into your page, let me know if
          > you get stuck.
          >
          > Do all 4 of these forms get submitted at some point? I'm curious.
          >
          > Mick
          >
          > Original function below.
          >
          > function verifyNoBlankEn tries(){
          >
          > //Loop through forms:
          > var f= document.forms. length;
          > while(f--){
          >
          > // loop through each form control:
          > fc=document.for ms[f].length;
          > for(k=0;k<fc;k+ +){
          >
          > // look for empty text boxes
          > if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
          > alert("empty field");documen t.forms[f][k].focus();return false;
          > }
          > }
          > }
          > return true;
          > }
          >
          >
          >[/color]


          Comment

          • Samir

            #6
            Re: checking and validating script

            Ohh and I don't really have 4 forms, I was just saying that for an example
            so I can use it on any form I have on any page.

            Well that's an interesting question, what would I need to do if I had four
            forms with different form names that needs checking and needed to be
            submitted. I figured it out with one form on a page, the form name is form,
            I just put document.form.s ubmit()
            "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
            news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...[color=blue]
            > Samir wrote:
            >[color=green]
            > > Thanks, it works perfect.
            > >
            > > One more thing, is it possible to add a description to a textbox so[/color][/color]
            that[color=blue][color=green]
            > > the warning can tell them what field it is??[/color]
            >
            > Yes it's possible (in most browsers), you could,for example, change the
            > empty field's background to shocking pink if you like. Where would you
            > like the description/warning to appear?
            >
            >
            > does this also work for drop[color=green]
            > > downs as well right?[/color]
            >
            >
            > No, but you can make sure that something is selected, or more precisely,
            > if the selection has a value:
            >
            > if(document.for ms[f][k].type=="select-one" &&[/color]
            !document.forms[f][k].value){[color=blue]
            > alert("Select something from the "+document.form s[f][k].name+" menu");
            > return false;
            > }
            >
            > Ur if you use the first slot in all of the menus for something like
            > "Select a ... ", you could rewrite the preceding code:
            >
            > if(document.for ms[f][k].type=="select-one" &&
            > !document.forms[f][k].selectedIndex) {alert( ...}
            >
            >
            > See if you can incorporate the code above into your page, let me know if
            > you get stuck.
            >
            > Do all 4 of these forms get submitted at some point? I'm curious.
            >
            > Mick
            >
            > Original function below.
            >
            > function verifyNoBlankEn tries(){
            >
            > //Loop through forms:
            > var f= document.forms. length;
            > while(f--){
            >
            > // loop through each form control:
            > fc=document.for ms[f].length;
            > for(k=0;k<fc;k+ +){
            >
            > // look for empty text boxes
            > if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
            > alert("empty field");documen t.forms[f][k].focus();return false;
            > }
            > }
            > }
            > return true;
            > }
            >
            >
            >[/color]


            Comment

            • Samir

              #7
              Re: checking and validating script

              One more thing

              Is it possible to put some kind a flag on a field so that the validation
              does not check it(non required field)?

              "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
              news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...[color=blue]
              > Samir wrote:
              >[color=green]
              > > Thanks, it works perfect.
              > >
              > > One more thing, is it possible to add a description to a textbox so[/color][/color]
              that[color=blue][color=green]
              > > the warning can tell them what field it is??[/color]
              >
              > Yes it's possible (in most browsers), you could,for example, change the
              > empty field's background to shocking pink if you like. Where would you
              > like the description/warning to appear?
              >
              >
              > does this also work for drop[color=green]
              > > downs as well right?[/color]
              >
              >
              > No, but you can make sure that something is selected, or more precisely,
              > if the selection has a value:
              >
              > if(document.for ms[f][k].type=="select-one" &&[/color]
              !document.forms[f][k].value){[color=blue]
              > alert("Select something from the "+document.form s[f][k].name+" menu");
              > return false;
              > }
              >
              > Ur if you use the first slot in all of the menus for something like
              > "Select a ... ", you could rewrite the preceding code:
              >
              > if(document.for ms[f][k].type=="select-one" &&
              > !document.forms[f][k].selectedIndex) {alert( ...}
              >
              >
              > See if you can incorporate the code above into your page, let me know if
              > you get stuck.
              >
              > Do all 4 of these forms get submitted at some point? I'm curious.
              >
              > Mick
              >
              > Original function below.
              >
              > function verifyNoBlankEn tries(){
              >
              > //Loop through forms:
              > var f= document.forms. length;
              > while(f--){
              >
              > // loop through each form control:
              > fc=document.for ms[f].length;
              > for(k=0;k<fc;k+ +){
              >
              > // look for empty text boxes
              > if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
              > alert("empty field");documen t.forms[f][k].focus();return false;
              > }
              > }
              > }
              > return true;
              > }
              >
              >
              >[/color]


              Comment

              • Mick White

                #8
                Re: checking and validating script

                Samir wrote:
                [color=blue]
                > Okay for example you have a textbox
                > <input type="text" name="T1" size="20">
                > Say...is there a way to add something like
                > <input type="text" name="T1" size="20" description="Fi rst Name">
                > So when they leave it blank, the form validation check would do an alert of
                > "You have left 'Then right here you would put the description of the
                > textbox'"
                > would something like that be possible?[/color]

                It would work on some browsers (IE and Safari, to name two), but you
                could use the "id" attribute to specify your description.
                <input type="text" name="T1" size="20" id="first name">

                if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""){
                alert("Please enter your
                "+document.form s[f][k].id+".");docume nt.forms[f][k].focus();return false;
                }
                A little kudgy though, better to give your form controls meaningful names.

                Kinda like you did for alert("Select[color=blue]
                > something from the "+document.form s[f][k].name+" menu");
                >
                > but is a description or some kind of field that could give a better
                > description? Same question goes for the drop down as well.[/color]

                You could assign your description to the first entry of your menu.

                <option value="Your description here"> Please select a colour</option>

                alert(document. forms[f][k].options[0].value)

                Mick
                [color=blue]
                >
                > Thanks again!
                >
                > "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
                > news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...
                >[color=green]
                >>Samir wrote:
                >>
                >>[color=darkred]
                >>>Thanks, it works perfect.
                >>>
                >>>One more thing, is it possible to add a description to a textbox so[/color][/color]
                >
                > that
                >[color=green][color=darkred]
                >>>the warning can tell them what field it is??[/color]
                >>
                >>Yes it's possible (in most browsers), you could,for example, change the
                >>empty field's background to shocking pink if you like. Where would you
                >>like the description/warning to appear?
                >>
                >>
                >> does this also work for drop
                >>[color=darkred]
                >>>downs as well right?[/color]
                >>
                >>
                >>No, but you can make sure that something is selected, or more precisely,
                >>if the selection has a value:
                >>
                >>if(document.f orms[f][k].type=="select-one" &&[/color]
                >
                > !document.forms[f][k].value){
                >[color=green]
                >>alert("Sele ct something from the "+document.form s[f][k].name+" menu");
                >>return false;
                >>}
                >>
                >>Ur if you use the first slot in all of the menus for something like
                >>"Select a ... ", you could rewrite the preceding code:
                >>
                >>if(document.f orms[f][k].type=="select-one" &&
                >>!document.for ms[f][k].selectedIndex) {alert( ...}
                >>
                >>
                >>See if you can incorporate the code above into your page, let me know if
                >>you get stuck.
                >>
                >>Do all 4 of these forms get submitted at some point? I'm curious.
                >>
                >>Mick
                >>
                >>Original function below.
                >>
                >>function verifyNoBlankEn tries(){
                >>
                >>//Loop through forms:
                >>var f= document.forms. length;
                >>while(f--){
                >>
                >>// loop through each form control:
                >>fc=document.f orms[f].length;
                >>for(k=0;k<fc; k++){
                >>
                >>// look for empty text boxes
                >>if(document.f orms[f][k].type=="text" && document.forms[f][k].value==""){
                >>alert("empt y field");documen t.forms[f][k].focus();return false;
                >>}
                >>}
                >>}
                >>return true;
                >>}
                >>
                >>
                >>[/color]
                >
                >
                >[/color]

                Comment

                • Mick White

                  #9
                  Re: checking and validating script

                  Samir wrote:
                  [color=blue]
                  > Ohh and I don't really have 4 forms, I was just saying that for an example
                  > so I can use it on any form I have on any page.
                  >
                  > Well that's an interesting question, what would I need to do if I had four
                  > forms with different form names that needs checking and needed to be
                  > submitted.[/color]

                  I don't know a foolproof way to do this.
                  Mick


                  I figured it out with one form on a page, the form name is form,[color=blue]
                  > I just put document.form.s ubmit()
                  > "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
                  > news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...
                  >[color=green]
                  >>Samir wrote:
                  >>
                  >>[color=darkred]
                  >>>Thanks, it works perfect.
                  >>>
                  >>>One more thing, is it possible to add a description to a textbox so[/color][/color]
                  >
                  > that
                  >[color=green][color=darkred]
                  >>>the warning can tell them what field it is??[/color]
                  >>
                  >>Yes it's possible (in most browsers), you could,for example, change the
                  >>empty field's background to shocking pink if you like. Where would you
                  >>like the description/warning to appear?
                  >>
                  >>
                  >> does this also work for drop
                  >>[color=darkred]
                  >>>downs as well right?[/color]
                  >>
                  >>
                  >>No, but you can make sure that something is selected, or more precisely,
                  >>if the selection has a value:
                  >>
                  >>if(document.f orms[f][k].type=="select-one" &&[/color]
                  >
                  > !document.forms[f][k].value){
                  >[color=green]
                  >>alert("Sele ct something from the "+document.form s[f][k].name+" menu");
                  >>return false;
                  >>}
                  >>
                  >>Ur if you use the first slot in all of the menus for something like
                  >>"Select a ... ", you could rewrite the preceding code:
                  >>
                  >>if(document.f orms[f][k].type=="select-one" &&
                  >>!document.for ms[f][k].selectedIndex) {alert( ...}
                  >>
                  >>
                  >>See if you can incorporate the code above into your page, let me know if
                  >>you get stuck.
                  >>
                  >>Do all 4 of these forms get submitted at some point? I'm curious.
                  >>
                  >>Mick
                  >>
                  >>Original function below.
                  >>
                  >>function verifyNoBlankEn tries(){
                  >>
                  >>//Loop through forms:
                  >>var f= document.forms. length;
                  >>while(f--){
                  >>
                  >>// loop through each form control:
                  >>fc=document.f orms[f].length;
                  >>for(k=0;k<fc; k++){
                  >>
                  >>// look for empty text boxes
                  >>if(document.f orms[f][k].type=="text" && document.forms[f][k].value==""){
                  >>alert("empt y field");documen t.forms[f][k].focus();return false;
                  >>}
                  >>}
                  >>}
                  >>return true;
                  >>}
                  >>
                  >>
                  >>[/color]
                  >
                  >
                  >[/color]

                  Comment

                  • Mick White

                    #10
                    Re: checking and validating script

                    Samir wrote:
                    [color=blue]
                    > One more thing
                    >
                    > Is it possible to put some kind a flag on a field so that the validation
                    > does not check it(non required field)?[/color]

                    I would add a suffix to the name or the id:

                    name="zipREQ"

                    if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""
                    && document.forms[f][k].name.indexOf(" REQ") != -1 )

                    Mick
                    [color=blue]
                    >
                    > "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
                    > news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...
                    >[color=green]
                    >>Samir wrote:
                    >>
                    >>[color=darkred]
                    >>>Thanks, it works perfect.
                    >>>
                    >>>One more thing, is it possible to add a description to a textbox so[/color][/color]
                    >
                    > that
                    >[color=green][color=darkred]
                    >>>the warning can tell them what field it is??[/color]
                    >>
                    >>Yes it's possible (in most browsers), you could,for example, change the
                    >>empty field's background to shocking pink if you like. Where would you
                    >>like the description/warning to appear?
                    >>
                    >>
                    >> does this also work for drop
                    >>[color=darkred]
                    >>>downs as well right?[/color]
                    >>
                    >>
                    >>No, but you can make sure that something is selected, or more precisely,
                    >>if the selection has a value:
                    >>
                    >>if(document.f orms[f][k].type=="select-one" &&[/color]
                    >
                    > !document.forms[f][k].value){
                    >[color=green]
                    >>alert("Sele ct something from the "+document.form s[f][k].name+" menu");
                    >>return false;
                    >>}
                    >>
                    >>Ur if you use the first slot in all of the menus for something like
                    >>"Select a ... ", you could rewrite the preceding code:
                    >>
                    >>if(document.f orms[f][k].type=="select-one" &&
                    >>!document.for ms[f][k].selectedIndex) {alert( ...}
                    >>
                    >>
                    >>See if you can incorporate the code above into your page, let me know if
                    >>you get stuck.
                    >>
                    >>Do all 4 of these forms get submitted at some point? I'm curious.
                    >>
                    >>Mick
                    >>
                    >>Original function below.
                    >>
                    >>function verifyNoBlankEn tries(){
                    >>
                    >>//Loop through forms:
                    >>var f= document.forms. length;
                    >>while(f--){
                    >>
                    >>// loop through each form control:
                    >>fc=document.f orms[f].length;
                    >>for(k=0;k<fc; k++){
                    >>
                    >>// look for empty text boxes
                    >>if(document.f orms[f][k].type=="text" && document.forms[f][k].value==""){
                    >>alert("empt y field");documen t.forms[f][k].focus();return false;
                    >>}
                    >>}
                    >>}
                    >>return true;
                    >>}
                    >>
                    >>
                    >>[/color]
                    >
                    >
                    >[/color]

                    Comment

                    • Samir

                      #11
                      Re: checking and validating script

                      Everything is going terrific!! Just to let you know that everything works.

                      You have been a great help.

                      "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
                      news:A5gHc.4165 9$iJ4.35236@twi ster.nyroc.rr.c om...[color=blue]
                      > Samir wrote:
                      >[color=green]
                      > > One more thing
                      > >
                      > > Is it possible to put some kind a flag on a field so that the validation
                      > > does not check it(non required field)?[/color]
                      >
                      > I would add a suffix to the name or the id:
                      >
                      > name="zipREQ"
                      >
                      > if(document.for ms[f][k].type=="text" && document.forms[f][k].value==""
                      > && document.forms[f][k].name.indexOf(" REQ") != -1 )
                      >
                      > Mick
                      >[color=green]
                      > >
                      > > "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
                      > > news:k90Hc.1579 4$yd5.10319@twi ster.nyroc.rr.c om...
                      > >[color=darkred]
                      > >>Samir wrote:
                      > >>
                      > >>
                      > >>>Thanks, it works perfect.
                      > >>>
                      > >>>One more thing, is it possible to add a description to a textbox so[/color]
                      > >
                      > > that
                      > >[color=darkred]
                      > >>>the warning can tell them what field it is??
                      > >>
                      > >>Yes it's possible (in most browsers), you could,for example, change the
                      > >>empty field's background to shocking pink if you like. Where would you
                      > >>like the description/warning to appear?
                      > >>
                      > >>
                      > >> does this also work for drop
                      > >>
                      > >>>downs as well right?
                      > >>
                      > >>
                      > >>No, but you can make sure that something is selected, or more precisely,
                      > >>if the selection has a value:
                      > >>
                      > >>if(document.f orms[f][k].type=="select-one" &&[/color]
                      > >
                      > > !document.forms[f][k].value){
                      > >[color=darkred]
                      > >>alert("Sele ct something from the "+document.form s[f][k].name+" menu");
                      > >>return false;
                      > >>}
                      > >>
                      > >>Ur if you use the first slot in all of the menus for something like
                      > >>"Select a ... ", you could rewrite the preceding code:
                      > >>
                      > >>if(document.f orms[f][k].type=="select-one" &&
                      > >>!document.for ms[f][k].selectedIndex) {alert( ...}
                      > >>
                      > >>
                      > >>See if you can incorporate the code above into your page, let me know if
                      > >>you get stuck.
                      > >>
                      > >>Do all 4 of these forms get submitted at some point? I'm curious.
                      > >>
                      > >>Mick
                      > >>
                      > >>Original function below.
                      > >>
                      > >>function verifyNoBlankEn tries(){
                      > >>
                      > >>//Loop through forms:
                      > >>var f= document.forms. length;
                      > >>while(f--){
                      > >>
                      > >>// loop through each form control:
                      > >>fc=document.f orms[f].length;
                      > >>for(k=0;k<fc; k++){
                      > >>
                      > >>// look for empty text boxes
                      > >>if(document.f orms[f][k].type=="text" && document.forms[f][k].value==""){
                      > >>alert("empt y field");documen t.forms[f][k].focus();return false;
                      > >>}
                      > >>}
                      > >>}
                      > >>return true;
                      > >>}
                      > >>
                      > >>
                      > >>[/color]
                      > >
                      > >
                      > >[/color][/color]


                      Comment

                      Working...