Validate Lotus Notes Domino fields using JavaScript

The following JavaScript function can be used to validate any Lotus Notes Domino field type -- for example, text, radio buttons, checkboxes, etc. One instance might be simply calling the JavaScript function from a submit button.

More on Lotus Notes and Domino development:
Can I use LotusScript or JavaScript to validate the date field on a Lotus Notes form?

Validating form fields using LotusScript

Compact LotusScript code for field validation

How to validate Lotus Notes forms on a Domino server without losing entered data

JavaScript Learning Guide

JavaScript Reference Center

The Lotus Notes Domino agent expects the form to have a field called RFields, which is a hidden, multi-valued text field that lists which Domino fields need to be validated.

RFields needs to be in the following format: <RequiredFieldName1>~<Field Description1>, <RequiredFieldName2>~<Field Description2>, etc.

An example of an RFields field would look like this:
"FName~First Name" : "LName~Last Name" : "Company~Company Name."

The agent parses each entry in RFields and checks the 'RequiredFieldName' to see if it contains any data. If it does not, it adds 'FieldDescription' to a text string, which is eventually displayed with an 'alert.' Here is the JavaScript code to accomplish this:

function CheckRequired() {
var rfields = new Array();
var fieldsplit = new Array();
temp = document.forms
rfields = temp.split(", ");
x = rfields.length
var msg = ""
var eles = document.forms
for (var i = 0; i <x; i++) { 
fieldsplit = rfields[i].split("~")
field = eles[fieldsplit[0]]
if( field.type == "select-one" ) {
if( (field.selectedIndex == -1) || 
(field.options[field.selectedIndex].text == "") ) 
msg = msg + fieldsplit[1] + "\n"  
if( field.type == "select-multiple" ) {
if( (field.selectedIndex == -1) || 
(field.options[field.selectedIndex].text == "") 
) msg = msg + fieldsplit[1] + "\n"

if( field.type == "text" ) {
y = field.value
if( y == "") msg = msg + fieldsplit[1] + "\n"
if( field.type == null ) {   // since radio button 
and checkbox fields are arrays of options,
 field.type returns null, so check field[0].type 
(the first // option of either the
 radio buttons or checkboxes
if( field[0].type == "radio" ) {
b = field.length
ok = false
for( var k = 0; k<b; k++) {
if ( field[k].checked ) ok = true
if( ok == false ) msg = msg + fieldsplit[1] + "\n"
if( field[0].type == "checkbox" ) {
b = field.length
ok = false
for( var k = 0; k<b; k++) {
if ( field[k].checked ) ok = true
if( ok == false ) msg = msg + fieldsplit[1] + "\n" 
}  // end for
if (msg != "") {
msg = "The following fields are required:\n\n" + msg

}  // end function

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Brad Nelson. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

This was first published in August 2007

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.