Validation function problem.

B

Bob Bedford

I've a function that validate a form field:

<html>
<head>
<script language="JavaScript">
function CheckFields(FormSubmit,BtnName)
{
if (BtnName!="Cancel")
{
check = true;
if (FormSubmit.User_Account.value == ""){
alert ("Username compulsory");
FormSubmit.User_Account.focus();
return false;
}....

My FORM:
<form action="<?php echo $editFormAction; ?>" name="formSubmit"
method="POST"> <!--form action myself-->
....
<input type="submit" name="SubmitButton" value="Add"
onClick="CheckFields(this.form, this.value)">
&nbsp;<input type="submit" name="SubmitButton" value="Cancel">

Now, if I click on cancel, I do redirect (with PHP) to a previous page. If I
click on Add, a check is done on the compulsory fields. I would like the
focus on the selected field (the one that isn't right), but the form is
reloaded. How to avoid this ?

I've tried to put the check on the form.OnSubmit (that fires before I submit
the form, and set the buttons input type as "button" instead of "submit"),
instead of the button click (that does submit the form), but I can't
retrieve with button was pushed. (I've tried FormSubmit.SubmitButton.name,
but it doesn't work)

Please help.
 
E

Eric Bohlman

<input type="submit" name="SubmitButton" value="Add"
onClick="CheckFields(this.form, this.value)">
&nbsp;<input type="submit" name="SubmitButton" value="Cancel">

Now, if I click on cancel, I do redirect (with PHP) to a previous
page. If I click on Add, a check is done on the compulsory fields. I
would like the focus on the selected field (the one that isn't right),
but the form is reloaded. How to avoid this ?

Your event handler needs to *return* the value from CheckFields:
onClick="return Checkfields..."
 
B

Bob Bedford

Thanks Eric...it was that simple !!!

Regards

Eric Bohlman said:
Your event handler needs to *return* the value from CheckFields:
onClick="return Checkfields..."
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top