Form Validation

Discussion in 'Javascript' started by sanju, Apr 7, 2008.

  1. sanju

    sanju Guest

    Dear All,

    i have 10 textboxes on a form, I require the following validation

    1) if i fill the textbox in between(say 5th), then all textboxes
    before that should be mandatory,ie Textboxes(1,2,3,4) should be
    mandatory and rest of the textboxes should not be mandatory.

    Kindly help me out..

    Thanks in advance..
    Regards,
    Sanjay
     
    sanju, Apr 7, 2008
    #1
    1. Advertising

  2. sanju

    Erwin Moller Guest

    sanju schreef:
    > Dear All,
    >
    > i have 10 textboxes on a form, I require the following validation
    >
    > 1) if i fill the textbox in between(say 5th), then all textboxes
    > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > mandatory and rest of the textboxes should not be mandatory.
    >
    > Kindly help me out..


    Well, what did you try so far?

    You should make a validationroutine that checks excactly that.
    What is the problem?
    Don't you know how to get the content of a textarea?
    Don't you know how to implement the logic to check the former X
    textelements for emptiness?
    Please state your problem clearer. :)

    Erwin Moller

    >
    > Thanks in advance..
    > Regards,
    > Sanjay
    >
     
    Erwin Moller, Apr 7, 2008
    #2
    1. Advertising

  3. sanju

    sanju Guest

    On Apr 7, 1:50 pm, Erwin Moller
    <> wrote:
    > sanju schreef:
    >
    > > Dear All,

    >
    > > i have 10 textboxes on a form, I require the following validation

    >
    > > 1) if i fill the textbox in between(say 5th), then all textboxes
    > > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > > mandatory and rest of the textboxes should not be mandatory.

    >
    > > Kindly help me out..

    >
    > Well, what did you try so far?
    >
    > You should make a validationroutine that checks excactly that.
    > What is the problem?
    > Don't you know how to get the content of a textarea?
    > Don't you know how to implement the logic to check the former X
    > textelements for emptiness?
    > Please state your problem clearer. :)
    >
    > Erwin Moller
    >
    >
    >
    >
    >
    > > Thanks in advance..
    > > Regards,
    > > Sanjay- Hide quoted text -

    >
    > - Show quoted text -



    Actually i dont know how to implement the logic to check the former
    X..
    Please help

    Thanks,
    sanjay
     
    sanju, Apr 7, 2008
    #3
  4. sanju

    SAM Guest

    sanju a écrit :
    > Dear All,
    >
    > i have 10 textboxes on a form, I require the following validation
    >
    > 1) if i fill the textbox in between(say 5th), then all textboxes
    > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > mandatory and rest of the textboxes should not be mandatory.



    "should be" or "have to be" ?

    Have to be :

    function validate(myForm) {
    var Cl = [];
    for(var i=0, L=myForm.length; i++)
    if(myForm.type= 'text') Cl[Cl.length] = myForm;
    if(Cl.length==10 && Cl[4].value!='') {
    for(var i=0; i<5; i++)
    if(Cl.value == '') {
    alert('textfield : '+Cl.name+\nis empty !');
    Cl.focus();
    return false;
    }
    for(var i=5; i<Cl.length; i++) Cl.value=='';
    }
    else {
    for(var i=5; i<Cl.length; i++)
    if(Cl.value == '') {
    alert('textfield : '+Cl.name+\nis empty !');
    Cl.focus();
    return false;
    }
    for(var i=0; i<5; i++) Cl.value=='';
    }
    return true;
    }

    <form onsubmit="return validate(this);" blah >

    --
    sm
     
    SAM, Apr 7, 2008
    #4
  5. sanju

    sanju Guest

    On Apr 7, 2:07 pm, SAM <>
    wrote:
    > sanju a écrit :
    >
    > > Dear All,

    >
    > > i have 10 textboxes on a form, I require the following validation

    >
    > > 1) if i fill the textbox in between(say 5th), then all textboxes
    > > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > > mandatory and rest of the textboxes should not be mandatory.

    >
    > "should be" or "have to be" ?
    >
    > Have to be :
    >
    > function validate(myForm) {
    > var Cl = [];
    > for(var i=0, L=myForm.length; i++)
    >    if(myForm.type= 'text') Cl[Cl.length] = myForm;
    > if(Cl.length==10 && Cl[4].value!='') {
    >     for(var i=0; i<5; i++)
    >       if(Cl.value == '') {
    >          alert('textfield : '+Cl.name+\nis empty !');
    >          Cl.focus();
    >          return false;
    >          }
    >     for(var i=5; i<Cl.length; i++) Cl.value=='';
    >     }
    > else {
    >     for(var i=5; i<Cl.length; i++)
    >       if(Cl.value == '') {
    >          alert('textfield : '+Cl.name+\nis empty !');
    >          Cl.focus();
    >          return false;
    >          }
    >     for(var i=0; i<5; i++) Cl.value=='';
    >     }
    > return true;
    >
    > }
    >
    > <form onsubmit="return validate(this);" blah >
    >
    > --
    > sm


    Dear Sam,

    Thanks a lot for the code..
    You have written this code by assuming that i filled 5th text box, but
    i want to fill any textbox between 1 to 10 and the textboxes before
    the filled one have to be mandatory.

    What shud i do?
    Thanks in advance
     
    sanju, Apr 7, 2008
    #5
  6. sanju

    SAM Guest

    sanju a écrit :
    >
    > Dear Sam,
    >
    > Thanks a lot for the code..
    > You have written this code by assuming that i filled 5th text box, but
    > i want to fill any textbox between 1 to 10 and the textboxes before
    > the filled one have to be mandatory.


    I do understand nothing at all
    - you told about the 5th
    - madatory = filled
    - which one before ? how much before ?
    - what about next fields ?

    function verif(form, what) {
    for(var i=0; i<form.length; i++)
    if(form == what && form[i-1].value == '') {
    alert('You missed textfield : '+form[i-1].name);
    form[i-1].focus();
    form[i-1].select();
    }
    }

    <input onkeyup="verif(this.form, this);" blah >
    <input onkeyup="verif(this.form, this);" blah >
    <input onkeyup="verif(this.form, this);" blah >
    <input onkeyup="verif(this.form, this);" blah >

    > What shud i do?


    don't use that it's not very hepful for visitor

    --
    sm
     
    SAM, Apr 7, 2008
    #6
  7. sanju schreef:
    > i have 10 textboxes on a form, I require the following validation
    >
    > 1) if i fill the textbox in between(say 5th), then all textboxes
    > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > mandatory and rest of the textboxes should not be mandatory.
    >


    This might help you:

    <html>
    <head>
    <title> Form validation </title>
    <script type="text/javascript">
    function checkInputs(form) {
    var inputs = document.getElementsByTagName('input');
    var notempty = false;
    for (i = inputs.length - 1; i > -1; i--) {
    if (inputs.type == 'text') {
    if (inputs.value.split(' ').join('')) {
    inputs.style.borderColor = '';
    notempty = true;
    continue;
    }
    if (notempty) {
    inputs.style.borderColor = 'red';
    } else {
    inputs.style.borderColor = '';
    }
    }
    }
    return false;
    }
    </script>
    </head>

    <body>
    <form onsubmit="return checkInputs(this.form)">
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="submit" value="submit"/>
    </form>
    </body>
    </html>


    JW
     
    Janwillem Borleffs, Apr 7, 2008
    #7
  8. sanju

    sanju Guest

    On Apr 7, 3:07 pm, SAM <>
    wrote:
    > sanju a écrit :
    >
    >
    >
    > > Dear Sam,

    >
    > > Thanks a lot for the code..
    > > You have written this code by assuming that i filled 5th text box, but
    > > i want to fill any textbox between  1 to 10 and the textboxes before
    > > the filled one have to be mandatory.

    >
    > I do understand nothing at all
    > - you told about the 5th
    > - madatory = filled
    > - which one before ? how much before ?
    > - what about next fields ?
    >
    > function verif(form, what) {
    > for(var i=0; i<form.length; i++)
    > if(form == what && form[i-1].value == '') {
    >    alert('You missed textfield : '+form[i-1].name);
    >    form[i-1].focus();
    >    form[i-1].select();
    >    }
    >
    > }
    >
    > <input onkeyup="verif(this.form, this);" blah >
    > <input onkeyup="verif(this.form, this);" blah >
    > <input onkeyup="verif(this.form, this);" blah >
    > <input onkeyup="verif(this.form, this);" blah >
    >
    > > What shud i do?

    >
    > don't use that it's not very hepful for visitor
    >
    > --
    > sm



    you r awesome Boss..
    you have done it in just few lines of code..
    hats off to u..
     
    sanju, Apr 7, 2008
    #8
  9. sanju

    sanju Guest

    On Apr 7, 3:14 pm, Janwillem Borleffs <> wrote:
    > sanju schreef:
    >
    > > i have 10 textboxes on a form, I require the following validation

    >
    > > 1) if i fill the textbox in between(say 5th), then all textboxes
    > > before that should be mandatory,ie Textboxes(1,2,3,4) should be
    > > mandatory and rest of the textboxes should not be mandatory.

    >
    > This might help you:
    >
    > <html>
    > <head>
    >         <title> Form validation </title>
    > <script type="text/javascript">
    >      function checkInputs(form) {
    >          var inputs = document.getElementsByTagName('input');
    >          var notempty = false;
    >          for (i = inputs.length - 1; i > -1; i--) {
    >              if (inputs.type == 'text') {
    >                  if (inputs.value.split(' ').join('')) {
    >                      inputs.style.borderColor ='';
    >                      notempty = true;
    >                      continue;
    >                  }
    >                  if (notempty) {
    >                      inputs.style.borderColor ='red';
    >                  } else {
    >                      inputs.style.borderColor ='';
    >                  }
    >              }
    >          }
    >          return false;
    >      }
    > </script>
    > </head>
    >
    > <body>
    > <form onsubmit="return checkInputs(this.form)">
    >      <input type="text"/>
    >      <input type="text"/>
    >      <input type="text"/>
    >      <input type="text"/>
    >      <input type="text"/>
    >      <input type="submit" value="submit"/>
    > </form>
    > </body>
    > </html>
    >
    > JW



    This is also one of the good solution
    Thanks boss

    regards,
    sanjay
     
    sanju, Apr 7, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Tommy
    Nov 29, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,207
    Sumith Mathur
    Sep 23, 2004
  3. OriginalBrownster

    string validation/ form validation

    OriginalBrownster, Aug 21, 2006, in forum: Python
    Replies:
    1
    Views:
    423
    Simon Forman
    Aug 21, 2006
  4. Grigory Temchenko
    Replies:
    0
    Views:
    332
    Grigory Temchenko
    Dec 28, 2008
  5. bnp
    Replies:
    4
    Views:
    326
Loading...

Share This Page