Form Validation Problem...Persisiting form fields on validation failure.

Discussion in 'Javascript' started by bnp, May 12, 2004.

  1. bnp

    bnp Guest

    Hi All,

    I am quite new the JavaScript. Basically I am a C++ programmer, but
    now I am working on JavaScript since last 5 days.

    I have a problem regarding the form validation.
    I have created a script that validates the form fields. the validation
    procedure is called ONCLICK event of the submit button.

    Follwowing is the structure of the validation procedure.
    ------------------------------------------
    // declaration for Submit Button
    <input type="submit" name="btnSubmit" onclick="validate()">

    function validate()
    {
    var bValidated = true;

    // check form fields.
    // If not valid then bValidated = false

    if (bValidated)
    {
    form.action = "BackEnd..php"
    form.method = "POST"
    form.submit();
    }
    else
    {
    return false;
    }
    -----------------------------------------

    Everythingg goews OK, if all fields are valid. But if any field
    contains Invalid data, the validate returns false, and all the fields
    in the form are reset, and user has to fill all the fields again.

    I want that, if validation fails, the fields which are valid should
    retain their values. How can I accomplish this?

    Which event is called, if validation procedure fails? IS it "onreset"?
    Help me soon.

    Thanks & Regards,

    Bhavik
     
    bnp, May 12, 2004
    #1
    1. Advertising

  2. bnp

    Tom Kiefer Guest

    The behavior you describe sounds rather strange. But rather than that, I'd
    point out that, instead of putting an onclick="validate()" in your submit
    button tag, try putting an onsubmit="return validate()" in your form tag.

    If the code within the form tag's onsubmit event handler attribute returns
    false, the form's submission action will be blocked, with no effect on the
    form or its contents. Remember that calling a function that returns false
    isn't enough; the event handler code has to return the value -- thus the
    "return validate()" rather than simply "validate()".

    Hope that helps.

    - Tom Kiefer
    thogek @ earthlink . net


    "bnp" <> wrote in message
    news:...
    > Hi All,
    >
    > I am quite new the JavaScript. Basically I am a C++ programmer, but
    > now I am working on JavaScript since last 5 days.
    >
    > I have a problem regarding the form validation.
    > I have created a script that validates the form fields. the validation
    > procedure is called ONCLICK event of the submit button.
    >
    > Follwowing is the structure of the validation procedure.
    > ------------------------------------------
    > // declaration for Submit Button
    > <input type="submit" name="btnSubmit" onclick="validate()">
    >
    > function validate()
    > {
    > var bValidated = true;
    >
    > // check form fields.
    > // If not valid then bValidated = false
    >
    > if (bValidated)
    > {
    > form.action = "BackEnd..php"
    > form.method = "POST"
    > form.submit();
    > }
    > else
    > {
    > return false;
    > }
    > -----------------------------------------
    >
    > Everythingg goews OK, if all fields are valid. But if any field
    > contains Invalid data, the validate returns false, and all the fields
    > in the form are reset, and user has to fill all the fields again.
    >
    > I want that, if validation fails, the fields which are valid should
    > retain their values. How can I accomplish this?
    >
    > Which event is called, if validation procedure fails? IS it "onreset"?
    > Help me soon.
    >
    > Thanks & Regards,
    >
    > Bhavik
     
    Tom Kiefer, May 12, 2004
    #2
    1. Advertising

  3. bnp

    pcx99 Guest

    Re: Form Validation Problem...Persisiting form fields on validationfailure.

    bnp wrote:

    > Which event is called, if validation procedure fails? IS it "onreset"?
    > Help me soon.


    If you have a reset button and the user clicks it then an onReset event
    is called. This isn't what you want.

    Try document.nameOfFormHere.reset()

    A more compatible method is probably...

    document.forms[0].reset();

    This will reset all the fields in the first form on the page, forms[1]
    will reset all the fields in the second form on the page, etc.

    --
    -------------
    http://www.hunlock.com -- DHTML for the rest of us.
     
    pcx99, May 12, 2004
    #3
  4. bnp

    Randy Webb Guest

    Re: Form Validation Problem...Persisiting form fields on validationfailure.

    bnp wrote:
    > Hi All,
    >
    > I am quite new the JavaScript. Basically I am a C++ programmer, but
    > now I am working on JavaScript since last 5 days.
    >
    > I have a problem regarding the form validation.
    > I have created a script that validates the form fields. the validation
    > procedure is called ONCLICK event of the submit button.
    >
    > Follwowing is the structure of the validation procedure.
    > ------------------------------------------
    > // declaration for Submit Button
    > <input type="submit" name="btnSubmit" onclick="validate()">
    >
    > function validate()
    > {
    > var bValidated = true;
    >
    > // check form fields.
    > // If not valid then bValidated = false
    >
    > if (bValidated)
    > {
    > form.action = "BackEnd..php"
    > form.method = "POST"
    > form.submit();
    > }
    > else
    > {
    > return false;
    > }
    > -----------------------------------------
    >
    > Everythingg goews OK, if all fields are valid. But if any field
    > contains Invalid data, the validate returns false, and all the fields
    > in the form are reset, and user has to fill all the fields again.
    >
    > I want that, if validation fails, the fields which are valid should
    > retain their values. How can I accomplish this?
    >
    > Which event is called, if validation procedure fails? IS it "onreset"?
    > Help me soon.


    No. Whether the validation works or not, the form gets submitted because
    you are not cancelling it.

    <form action="something.ext" onsbumit="return validate()">

    And then have your validate function return true or false.



    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
     
    Randy Webb, May 12, 2004
    #4
  5. bnp

    bnp Guest

    Hi,

    It's done.
    Thanks for your help.

    Regards,

    Bhavik

    "Tom Kiefer" <> wrote in message news:<6gioc.17042$>...
    > The behavior you describe sounds rather strange. But rather than that, I'd
    > point out that, instead of putting an onclick="validate()" in your submit
    > button tag, try putting an onsubmit="return validate()" in your form tag.
    >
    > If the code within the form tag's onsubmit event handler attribute returns
    > false, the form's submission action will be blocked, with no effect on the
    > form or its contents. Remember that calling a function that returns false
    > isn't enough; the event handler code has to return the value -- thus the
    > "return validate()" rather than simply "validate()".
    >
    > Hope that helps.
    >
    > - Tom Kiefer
    > thogek @ earthlink . net
    >
    >
    > "bnp" <> wrote in message
    > news:...
    > > Hi All,
    > >
    > > I am quite new the JavaScript. Basically I am a C++ programmer, but
    > > now I am working on JavaScript since last 5 days.
    > >
    > > I have a problem regarding the form validation.
    > > I have created a script that validates the form fields. the validation
    > > procedure is called ONCLICK event of the submit button.
    > >
    > > Follwowing is the structure of the validation procedure.
    > > ------------------------------------------
    > > // declaration for Submit Button
    > > <input type="submit" name="btnSubmit" onclick="validate()">
    > >
    > > function validate()
    > > {
    > > var bValidated = true;
    > >
    > > // check form fields.
    > > // If not valid then bValidated = false
    > >
    > > if (bValidated)
    > > {
    > > form.action = "BackEnd..php"
    > > form.method = "POST"
    > > form.submit();
    > > }
    > > else
    > > {
    > > return false;
    > > }
    > > -----------------------------------------
    > >
    > > Everythingg goews OK, if all fields are valid. But if any field
    > > contains Invalid data, the validate returns false, and all the fields
    > > in the form are reset, and user has to fill all the fields again.
    > >
    > > I want that, if validation fails, the fields which are valid should
    > > retain their values. How can I accomplish this?
    > >
    > > Which event is called, if validation procedure fails? IS it "onreset"?
    > > Help me soon.
    > >
    > > Thanks & Regards,
    > >
    > > Bhavik
     
    bnp, May 12, 2004
    #5
    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. Lloyd Sheen
    Replies:
    8
    Views:
    427
    William LaMartin
    Nov 19, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,219
    Sumith Mathur
    Sep 23, 2004
  3. Ilias Lazaridis
    Replies:
    0
    Views:
    593
    Ilias Lazaridis
    Feb 1, 2005
  4. Replies:
    0
    Views:
    494
  5. Replies:
    23
    Views:
    545
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page