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

B

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
 
T

Tom Kiefer

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
 
P

pcx99

bnp said:
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.
 
R

Randy Webb

bnp said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top