This tip describes three quick methods for validating a rich text field in Lotus Notes. The first method validates a text field in which any text (including a single space character) is an acceptable input. The second method validates a field in which at least some non-space text is required. The third method validates a field that may contain no text, but may contain an attachment, embedded object, or link. We assume that you have some experience developing applications using Domino Designer and LotusScript.
In the first method, if the field contains any input (even only a single space character), then it can pass the validation. This validation uses the Querysave event of the form that contains the field. The following LotusScript sample code performs this validation:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
If ( Source.FieldGetText( "rtfield" ) = "" ) Then
Messagebox( "Please enter some text." )
Call Source.GotoField( "rtfield" )
Continue = False
End If
End Sub
|
The code checks for any character in the field rtfield. If the field contains input (even if it consists solely of one or more space characters), then validation succeeds. If the field is empty, then the code returns an error message, and doesn’t save the document (by setting Continue to False).
In the second method, the rich text field must contain at least one non-space character (in other words, an input consisting entirely of one or more spaces is not allowed). This check also uses the Querysave event of the form:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
Dim rtitem As NotesRichTextItem
Set doc = Source.Document
Set rtitem = doc.GetFirstItem( "rtfield" )
Dim text As String
text$ = Source.FieldGetText("rtfield")
trimmed$ = Trim(text)
if ( trimmed$ = "") Then
Msgbox "Please enter some text."
Continue = False
source.GotoField("rtfield")
source.Refresh(True)
Else
Continue = True
End If
End Sub
|
Our third method validates a rich text field in which an input consisting solely of an attachment, embedded object, or link is allowed, even if it includes no accompanying text. Once again, we use the Querysave event of the form containing the field:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
Dim rtitem As NotesRichTextItem
Set doc = Source.Document
Set rtitem = doc.GetFirstItem( "rtfield" )
Dim text As String
text$ = Source.FieldGetText("rtfield")
trimmed$ = Trim(text)
If(doc.Hasembedded) Then
Continue = True
Elseif ( trimmed$ = "") Then
Msgbox "Please enter some text."
Continue = False
source.GotoField("rtfield")
source.Refresh(True)
Else
Continue = True
End If
End Sub
|
This code will work if there is an attachment anywhere in the document, even if it's not in the field that is being validated.
- Participate in the discussion forum.
-
The Lotus Notes product page is a good resource for information about Lotus Notes.
-
Learn more about Domino Designer by reading the Domino Designer documentation.
-
Participate in developerWorks
blogs and get involved in the developerWorks community.




