Javascript to "input validate" a combobox, replace a field value, save the doc, and redirect to a url?

StartTime is the combobox which allows the user to select a time of day,StartTimeAllDay is a checkbox which (when checked) I want to set the value of StartTime to “08:00 AM”

my script is supposed to check for a value in the StartTime combobox or that the StartTimeAllDay checkbox is checked. If no value is selected in either, the user should get an error message indicating as such, the form should not submit and the surcor should go to the StartTime combobox,

The following script simply submits the form allowing the null values in StartTime:

if (document.forms[0].StartTimeAllDay.checked)

{

document.forms[0].StartTime = document.forms[0].StartTimeAllDay.value;

}

if (document.forms[0].StartTimeAllDay.unchecked && document.forms[0].StartTime.selectedIndex == -1)

{

alert(“Please select a start time.”);

document.forms[0].StartTime.focus();

return false;

}

document.forms[0].submit();

I’d also like to (after the form is submitted), redirect the user to another URL, sort of an @URLOpen equivalent,

any ideas?

tks,

-MC

Subject: SOLUTION: javascript to “input validate” a combobox, replace a field value, save the doc, and redirect to a url?

This code is for a group calendar application and does the following:

  1. Provides input validation to the following fields:

EventTitle

StartDate

EndDate

If any of the fields have not been populated, a notification box appears and the cursor is placed within the field in need of a value.

  1. If the user has checked the StartTimeAllDay checkbox, the StartTime combobox field gets automatically assigned the earliest start time.

If the user has checked the EndTimeAllDay checkbox, the EndTime combobox field gets automatically assigned the latest end time.

  1. Submits the form (or document).

  2. Redirects the user automatically to the main calendar view open by month.

— BEGIN JAVASCRIPT CODE IN SUBMIT BUTTON —

sisChecked = false;

eisChecked = false;

if (form.StartTimeAllDay.checked ) {

sisChecked = true;

}

if (form.EndTimeAllDay.checked ) {

eisChecked = true;

}

if (document.forms[0].EventTitle.value==“”)

{

alert(“Please enter an event title.”);

document.forms[0].EventTitle.focus();

return false;

}

if (document.forms[0].StartDate.value==“”)

{

alert(“Please select a start date.”);

document.forms[0].StartDate.focus();

return false;

}

if (document.forms[0].StartTime.selectedIndex == 0 && ! sisChecked

) {

alert(“Please select a start time.”);

document.forms[0].StartTime.focus();

return false;

}

if (document.forms[0].EndDate.value==“”)

{

alert(“Please select an end date.”);

document.forms[0].EndDate.focus();

return false;

}

if (document.forms[0].EndTime.selectedIndex == 0 && ! eisChecked

) {

alert(“Please select an end time.”);

document.forms[0].EndTime.focus();

return false;

}

if (sisChecked)

{

document.forms[0].StartTime.selectedIndex = 1;

}

if (eisChecked)

{

document.forms[0].EndTime.selectedIndex = 26;

}

document.forms[0].submit();

— END JAVASCRIPT CODE IN SUBMIT BUTTON —

To redirect the user after form submission, I added a $$Return field with the following code:

“[http://www.domain.com” + “/” + @ReplaceSubstring(@Subset(@DbName;-1);“\”;“/”) +“/calendar?OpenView&Grid=4]”

NOTE: The beginning and ending quotation marks should be included

Hope this helps someone,

-MC

Subject: javascript to “input validate” a combobox, replace a field value, save the doc, and redirect to a url?

Hi Michael,

I’m not sure that I’m responding to the entirety of your issue; but, regarding redirect on form submission, you can try one of the following:

Method One

Add a $$Return field to your form, and use the redirect URL as the Default Value of this field (which should be computed, and hidden). For example, to redirect users to http://www.lotus.com on form submission, use the following as the Default Value of $$Return: “[http://www.lotus.com]” (brackets are necessary).

Method Two

Write a WebQuerySave agent that will generate a page, dynamically, on form submission. The sole purpose of this page is to redirect the user to another URL. The code for the agent would look like this:

print ||

There are other ways you can do this (for instance, you could write a formula based Web Query Agent that used @URLOpen), but the two above are the ones that I tend to use most.

:slight_smile:

Hope it helps. Happy Monday.

Cate

Subject: thanks Cate, that solves the redirect issue

now if I could just get this javascript validation working I’ll be set,

tks,

-MC

Subject: RE: thanks Cate, that solves the redirect issue

Michael,

Probably the easiest way to determine where the problem lies would be to simplify things by testing only one of the validation conditions at a time. If one of them works (the checkbox validation, for example), then the other one is probably what’s causing the function to fail.

So, alter your code to do some testing. Remove the first condition, and test only the second. If that works as desired, then remove the second, and test the first. Most likely that’s the one that will fail. If so, then you know where to start troubleshooting. However, if each condition works fine on its own, then perhaps there’s a syntax problem in your code.

If it were me, I would be suspicious of the ‘unchecked’ property of the checkbox object. I’ve never seen it used, before, and though it definitely makes sense, in the past it’s just been common practice for developers to use custom functions like ‘getSelectedCheckbox(checkbox_object)’ to fetch the value of a checkbox. But, maybe ‘unchecked’ isn’t the problem. If so, then I’ve learned something new, today.

:slight_smile:

Cate

PS - In case you end up debugging the checkbox validation, here’s a checkbox function. I’ve used this before, but it’s not mine. This was posted on the Internet, so if you use it, be sure to give credit for it (though I have no idea where it came from, originally).

function getSelectedCheckbox(buttonGroup) {

// Go through all the check boxes. return an array of all the ones

// that are selected (their position numbers). if no boxes were checked,

// returned array will be empty (length will be zero)

var retArr = new Array();

var lastElement = 0;

if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)

  for (var i=0; i<buttonGroup.length; i++) {

     if (buttonGroup[i].checked) {

        retArr.length = lastElement;

        retArr[lastElement] = i;

        lastElement++;

     }

  }

} else { // There is only one check box (it’s not an array)

  if (buttonGroup.checked) { // if the one check box is checked

     retArr.length = lastElement;

     retArr[lastElement] = 0; // return zero as the only array value

  }

}

return retArr;

} // Ends the “getSelectedCheckbox” function