title: ServiceNow Script onSubmit not working properly
body: <p>I am using ServiceNow platform. I am writing a Catalog Client Script to validate form fields on a Catalog Item record producer.</p>
  
  <p>I am stopping the submission of the form by using return false if validation does not pass inspection.</p>
  
  <p>I have tested this by entering invalid data (group name with special characters or a group name that exists already) and it catches the issue and shows the error message. I can enter invalid data and submit multiple times and it works.</p>
  
  <p>However, the issue:</p>
  
  <p>The script seems to "stop" running after I first enter invalid data and submit, and then I correct the data press the submit button again. It just sits there and does nothing. I have to reload the form again which is not desirable.</p>
  
  <p>What is going on with the control flow? How can I cleanly stop the form if the data is invalid, but then allow the user to correct the mistake and press the submit button again to proceed?</p>
  
  <p>I can tell that the script doesn't run again because I have an alert box popping up that says "script run" every time the script runs. It just stops running at some point after submitting invalid data first and then entering some valid data and pressing submit.</p>
  
  <pre><code>function onSubmit() {
      g_form.hideAllFieldMsgs('error');
      alert("script run");
  
      //Group Name contain letters numbers and dashes only
      var group_name = g_form.getValue('u_group_name');
      // Group name regular expression
      var regGrpName = /^([A-Za-z0-9\-]+)$/;
      // Check name against regular expression
      if (regGrpName.test(group_name) == false) {
          g_form.showFieldMsg('u_group_name', "Group Name must contain only letters, numbers or dashes. ", 'error');
          //Do not submit
          //g_form.submitted = false;
          return false;
      }
  
      //Check if google group already exists
      var rec = new GlideRecord('u_google_user_accounts');
      rec.addQuery('u_account_email', new_group_email);
      rec.query();
  
      while (rec.next()) {
          g_form.showFieldMsg('u_group_name',rec.u_account_email + " already exists as an account.",'error');
          return false;
      }
  
      //Group Members Email List separated by commas
      // Hide error message
      //g_form.hideErrorBox('u_group_members');
      var group_members = g_form.getValue('u_group_members');
      // Comma separate list
      var member_split = group_members.split(',');
      // Loop over list of email addresses
      for (var n = 0; n &lt; member_split.length; n++) {
          // Trim whitespace
          var member_info = trim ? member_split[n].trim() : member_split[n];
          // Email validation regular expression
          var regEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
          // Check each item against regular expression
          if (member_info.search(regEmail) == false) {
              g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " + member_info, 'error');
              //Do not submit
              //g_form.submitted = false;
              return false; 
          } else if (member_info.search(validRegExp) == true) {
              g_form.setValue('u_group_members', group_members);  
          }
      }
      return true;
  }
  </code></pre>