Input Validation
Domino developers have long used the Notes input-validation field property to validate their field data. JavaScript offers yet another method to check field data for validation.
The following example uses JavaScript for the form's onChange event, and the code is called from the JS Header . Why do it this way? Have you ever encountered a validation error and not known what field it is for? This can cause a developer much wasted time and effort searching and screening each field on the form to debug it. By placing your code in one central location, you can quickly and easily find all your input validations. This also allows you to program your error messages so that you can explicitly name the field throwing the exception.
Here's an input-validation JavaScript function placed in the JS Header on a problem report form in Domino:
//The Input Validation Function for all required fields on a form.
function validate(){
var validatemsg;
var validateflag;
validateflag = 'false';
if(document.forms[0].Caller.value == ''){
validatemsg='CALLER FIELD ERROR: Please select a caller name in the caller
This validation code steps through each field on a problem report form and checks to make sure that the required field data is filled in. If the function encounters a field in which the data is not filled in, the user is prompted by an error message indicating the field name, the problem, and the user returned to the appropriate field to correct their error. When the field passes the validation check and the validateflag variable equals false (there are no errors), the document is saved. Figure 16.18 shows what this code looks like inside the JS Header of the Designer.
Figure 16.18. The validation code in the JS Header of the Designer.
To call the validate() function, simply place the function call in an action button or event on the form. Figure 16.19 shows a Save and Close action button along with its onClick function call in the Programmer's pane.
Figure 16.19. The Save and Close action buttons in the JS Header of the Designer.
You can also call the function in the form's onSubmit event. Either way, the fields are validated using the validate() JavaScript function.