Thought I’d share something that took me a while to figure out.
If you have a form with multiple buttons on it, and Javascript code providing form validation in the onSubmit event, sometimes you need to modify the behavior of that validation code depending on which button was pressed. For example, you might have a button that changes some aspect of the form, while another that actually submits the form. Because Domino buttons all share the same onSubmit code and name, however, there doesn’t seem to be a way to determine which button was pressed so that your Javascript can work differently for the different buttons.
There is a way around this problem by using the onFocus event of the button to modify a flag. The following is a step-by-step procedure on how to do this:
Assumptions:
-
A Domino form with two buttons on it and at least one field (in this case, one named ADDR, and the following html code after the field:
) -
Javascript code like the following in the onSubmit button:
var checkform = true;
var doc = document.forms[0];
var mdiv;
var pmsg = ‘’;
// ADDRESS
mdiv = document.getElementById(“AddrMsg”);
if (doc.ADDR.value==“”)
{
mdiv.innerHTML = "<span class='Error'>« An <b>Address</b> is required</span>";
pmsg += "\n Address is required";
checkform = false;
doc.ADDR.focus();
}
else
{
mdiv.innerHTML="";
}
if (pmsg!=“”) {
checkform = false;
alert(pmsg);
}
// FINAL FORM CHECK
if (checkform)
{
document.forms[0].submit;
return true;
}
else
{
return false;
}
When any button is pressed on this form, this code executes BEFORE the Domino code to process the form, providing an opportunity to validate whether a field has a value in it before sending the form to the server. However, in this example there are two buttons, one to submit the entire form and another button tied to hide-when logic on parts of the form that hides or displays other parts of the form depending on a picklist selection. Normally, then, clicking either button would cause this logic to be executed, which means that the entire form would have to validate correct before the button that simply changes the form could be pressed by the user - which isn’t intuitive to the user.
To correct that problem do the following:
-
Create an editable field on the form called BTYPE (or any other name as long as you adjust the code appropriately in the next step) with the code Type=“Hidden” in the Other tag field on the HTML tab of the field properties. This creates a field that isn’t displayed on the screen, but can be accessed by Javascript code
-
Assign a number to each button on the form. In the HTML properties for that button, place code such as the following in the Other tag field: onfocus=“document.forms[0].BTYPE.value=‘1’”. Use a different value assignment for each button. This code is a Javascript snippet that sets the field value to the specified string when form focus is transferred to the button. Interestingly enough, when you click a button you fire (at least) two events: onFocus and onSubmit. We are simply using the first to update a value on the form.
-
Modify the code on the onSubmit button as follows:
var checkform = true;
var doc = document.forms[0];
var mdiv;
var pmsg = ‘’;
var bpressed = doc.BTYPE.value; // Button Press 1=Update, 2=Save, 3=MakeChanges
// IF UPDATE BUTTON PRESSED, BYPASS
if (bpressed==“1”)
{
document.forms[0].submit;
return true;
}
// ADDRESS
mdiv = document.getElementById(“AddrMsg”);
if (doc.ADDR.value==“”)
{
mdiv.innerHTML = "<span class='Error'>« An <b>Address</b> is required</span>";
pmsg += "\n Address is required";
checkform = false;
doc.ADDR.focus();
}
else
{
mdiv.innerHTML="";
}
if (pmsg!=“”) {
checkform = false;
alert(pmsg);
}
// FINAL FORM CHECK
if (checkform)
{
document.forms[0].submit;
return true;
}
else
{
return false;
}
The changes made here simple tell the Javascript validation code to be ignored if a specific button was pressed, but to perform validation otherwise. Your code may vary significantly depending on what behavior you are trying to model.
– Main.ChristopherBobbitt - 15 Jan 2007