By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

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

P: n/a
bnp
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <bh**********@yahoo.com> wrote in message
news:7d*************************@posting.google.co 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

Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

P: n/a
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/
Jul 23 '05 #4

P: n/a
bnp
Hi,

It's done.
Thanks for your help.

Regards,

Bhavik

"Tom Kiefer" <th*******@earthlinknot.net> wrote in message news:<6g*******************@newsread2.news.pas.ear 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" <bh**********@yahoo.com> wrote in message
news:7d*************************@posting.google.co 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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.