Validating a Required Field

Suppose a user leaves the Name field on the Group form blank. An entry is required in the Name field (the required attribute is set to TRUE).

Clicking Change to submit the form calls the update function, which is the onSubmit event handler. The following code fragment shows the event handler for the onSubmit event of the Group form:

<form name="frmGroup" action="../Scripts/postIt.asp" method="post" target="postIt" onSubmit="return update()" onReset="refresh()">

Next, the update function calls the validateForm function in IsValid.js and passes the form (frmGroup) object as a parameter. If all fields on the form are successfully validated, the function returns a value of TRUE; otherwise, the function returns a value of FALSE and submission of the form stops. The following code fragment illustrates this process:

   var theForm = document.frmGroup;
   if(validateForm(theForm)){
…
   return true;
   }
   else
      return false;

The validateForm function loads the elements on the form into an array and loops through the array, checking for elements with a required attribute that are blank. If a required element is blank, a localized message is displayed, the field that requires an entry receives the cursor, the function returns a value of FALSE, and submission of the form stops. The following code fragment illustrates this process:

if(elArr[i].required=="true"){
        if(Trim(value)==''){
        //L_NAME(expando attrib) is localized version of name attribute
        alert(L_Empty + elArr[i].L_NAME); 
        elArr[i].focus();
        return false;