Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Form Validation Problem...Persisiting form fields on validation failure.

Reply
Thread Tools

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

 
 
bnp
Guest
Posts: n/a
 
      05-12-2004
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
 
Reply With Quote
 
 
 
 
Tom Kiefer
Guest
Posts: n/a
 
      05-12-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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



 
Reply With Quote
 
 
 
 
pcx99
Guest
Posts: n/a
 
      05-12-2004
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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      05-12-2004
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/
 
Reply With Quote
 
bnp
Guest
Posts: n/a
 
      05-12-2004
Hi,

It's done.
Thanks for your help.

Regards,

Bhavik

"Tom Kiefer" <(E-Mail Removed)> wrote in message news:<6gioc.17042$(E-Mail Removed) thlink.net>...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
epydoc: How to add new fields as the building fields? Cyril.Liu Python 0 12-02-2008 05:01 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
Struts validation for combine form fields validation Sumith Mathur Java 0 09-23-2004 06:26 PM
how to copy from fields to other fields middletree ASP General 1 11-05-2003 06:24 PM
Netscape hidden fields - array - multiple fields with same name mark.reichman@rl.af.mil Javascript 0 07-17-2003 03:05 PM



Advertisments