Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with form verification 1

Status
Not open for further replies.

Skee777

MIS
Nov 29, 2001
17
US
I am trying to validate a form using javascript. The form has two submit buttons. If the first button is clicked, the form should run a brief verification, then change the form action to an asp page. If the second button is clicked, the for will run the verification, then change the form action to a different page. The verification is simply to verify that the field is not empty.

What happens now is that if you try to submit the form with either button, an alert box appears, then when you clear that alert box, another alert box appears. Finally, when you click either submit button, both buttons take you to the first action page.

Below is the code I am trying to use. It is almost like the if else portion is not functioning.

Any suggestions would be great! Thanks!



<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
// this script is used to change the action of the form
// dependent on which button you select

function checkform ( form )
{
if ((document.office.BranchCode.value != &quot;&quot;) && (document.office.S1.value == &quot;Create New Entry&quot;)) {
document.office.action = &quot;itemlist.asp&quot;;
return true;
}
else if ((document.office.BranchCode.value != &quot;&quot;) && (document.office.S2.value == &quot;Modify Existing Records&quot;)) {
document.office.action = &quot;itemmodify.asp&quot;;
return true;
}
else {
alert( &quot;Please Select an Item.&quot; );
document.office.BranchCode.focus();
return false ;
}
}
//-->
</SCRIPT>
 
What are S1 and S2? are they your submit buttons?

If they are then your code will always execute the first branch of the control (the code within the first IF), that is if the value of S1 is &quot;Create New Entry&quot;.

The value of a submit button does not depend on whether it's clicked or not; If your HTML code looks something like this:

<input type=&quot;submit&quot; name=&quot;S1&quot; value=&quot;Create New Entry&quot;>

then the expression

document.office.S1.value == &quot;Create New Entry&quot;

will evaluate to true regardless of whether S1 or S2 was pressed.

To change the action depending on what button was pressed, you could do something like this. (i prefer to use regular buttons rather than submit buttons and explicitly call the submit method)

<input type=&quot;button&quot; value=&quot;Create New Entry&quot; onClick=&quot;checkForm('itemlist.asp')&quot;>

<input type=&quot;button&quot; value=&quot;Modify Existing Records&quot; onClick=&quot;checkForm('itemmodify.asp')&quot;>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
// this script is used to change the action of the form
// dependent on which button you select

function checkform ( inputAction)
{
if (document.office.BranchCode.value == &quot;&quot;){
alert( &quot;Please Select an Item.&quot; );
document.office.BranchCode.focus();
return false ;
}
else{
document.office.action = inputAction;
document.office.submit();
}
}
//-->
</SCRIPT>

You could also pass a reference to the button into the function [ onClick=&quot;checkForm(this)&quot; ]
and in your function [ function checkForm(inputElement) ]
check inputElement.value against the strings &quot;Create New Entry&quot; & &quot;Modify Existing Records&quot;

hope this helps

cheyney


 
Thank you so much. Your explaination was very thorough and very helpful. I hadn't considered the fact that the S1 value would always be true.

Your assistance us very appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top