← Script library

Client Scripts

Prevent Form Submission

Validate form data and prevent submission if conditions aren't met (e.g., prevent closing ticket without resolution notes).

JavaScript
function onSubmit() {
  // Configuration: Define validation rules
  var state = g_form.getValue('state');
  var closedStates = ['6', '7', '8'];  // Resolved, Closed, Cancelled
  var requiredFields = {
    'close_code': 'Close Code',
    'close_notes': 'Close Notes'
  };

  // Check if ticket is being closed
  if (closedStates.indexOf(state) !== -1) {

    // Validate each required field
    for (var fieldName in requiredFields) {
      var fieldValue = g_form.getValue(fieldName);
      var fieldLabel = requiredFields[fieldName];

      // If field is empty, show error and prevent submission
      if (!fieldValue || fieldValue === '') {
        g_form.addErrorMessage(fieldLabel + ' is required when closing an incident.');
        g_form.flash(fieldName, '#FF0000', 0);  // Flash the field red indefinitely
        return false;  // Prevent form submission
      }
    }

    // Additional validation: Check minimum length for close notes
    var closeNotes = g_form.getValue('close_notes');
    if (closeNotes.length < 10) {
      g_form.addErrorMessage('Close Notes must be at least 10 characters long.');
      g_form.flash('close_notes', '#FF0000', 0);
      return false;
    }
  }

  // Validation passed, allow form submission
  return true;
}

How to use it

1. Create an onSubmit Client Script on your table 2. Customize the validation logic for your requirements 3. Update field names and validation rules 4. Test thoroughly to ensure it works as expected

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.