232 views

Description

Service Portal: No validators for IP, Email, Duration, and URL type variables.

 

Steps to Reproduce

 

  1. Create a catalog item with the following types of variables:

    • IP
    • Address
    • Duration
    • Email
    • URL

    For more information, see the product documentation topic Create a service catalog variable.

  2. Launch the Service Portal instance and navigate to the newly created catalog item.

  3. Try to give incorrect values to the newly created types of variables and observe the result.

    Expected behavior: The validators should be fired indicating the user that the entered value is incorrect with the help of some alert or messages.

    Actual behavior: No validators given for these variable types in Service Portal.

 

 

Workaround

Use onChange catalog client scripts to validate the input. You can use either a simple script when only a few variables need to be validated or a more robust and centralized approach for a larger installation.

Small Installation

The following example demonstrates this approach for an Email variable. The script uses a regular expression to validate the email address, and displays a notification if the user inputs an invalid value. This sample script can be applied to the other variable types by using different regular expressions that are applicable to that type.

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') return;
  
  function validateEmail ( email ) {
    
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test( email.toLowerCase() );
  }
  
  if ( !window ) { // Only run in Service Portal
    
    var variableName = 'valid_email'; // Update this to match your variable's name
    
    g_form.hideFieldMsg( variableName, 'true' ); // Hide any existing field messages
  
    if ( !(validateEmail( newValue )) ) {
    
      g_form.showFieldMsg( variableName, 'Invalid email address: '+newValue, 'error', false );
      g_form.setValue( variableName, '' ); // Clear out the invalid value
    }
  }
}

 

Larger Installation

Larger implementations necessitate a more robust and centralized approach. You can define the validation function in a script include and abstract it to handle all four of the affected variable types, as shown in the following example.

  

function validateVariable ( val, type ) {

  var regExs = {

    email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
       ip: /.../, 
      dur: /.../,
      url: /.../
  };

  return regExs[type].test( val.toLowerCase() );
}

 

Client scripts can be simplified to call the same function via GlideAjax. If you need assistance with creating the regular expressions for the other variable types, consider searching online for a regex generator site to make them for you.

 

 


Related Problem: PRB957575

Seen In

There is no data to report.

Associated Community Threads

There is no data to report.

Article Information

Last Updated:2018-06-18 08:45:19
Published:2018-01-04