Validate Lotus Notes Domino fields using JavaScript

Learn how to use JavaScript to validate any type of Lotus Notes Domino field types, including text, buttons and checkboxes, courtesy of SearchDomino.com member Brad Nelson.

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
[0].RFields.value;
rfields = temp.split(", ");
x = rfields.length
var msg = ""
var eles = document.forms
[0].elements
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
 alert(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

Dig deeper on JavaScript for Lotus Notes Domino

0 comments

Oldest 

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:

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.uk

Close