restrict user to enter negative values and special char in record producer

Priyansh_98
Tera Guru

how can i restrict a user to restrict to enter a negative values and special character in record producer?

1 REPLY 1

Rajesh_Singh
Kilo Sage
Kilo Sage

@Priyansh_98 

 

To restrict a user from entering negative values and special characters in a record producer in ServiceNow, you can use client-side scripting to validate the input before it is submitted to the server.

Here's an example of how you can achieve this using Javascript:

  1. Open the record producer that you want to apply the validation to in the ServiceNow form designer.
  2. In the client script section of the form, create a new script and add the following code:
function onSubmit() {
   var inputField = g_form.getValue('field_name'); // replace field_name with the name of the field you want to validate
   var regex = /^[0-9]+$/; // allow only positive integers
   if (!regex.test(inputField)) {
      alert('Please enter a positive integer value'); // display an error message if the input is invalid
      return false; // prevent the form from being submitted
   }
   return true; // allow the form to be submitted if the input is valid
}
  1. Save the script and test the record producer to ensure that it is properly restricting negative values and special characters.

In the example above, we are using a regular expression to test the input field for positive integers only. You can modify the regular expression to suit your specific validation requirements, such as allowing decimal values or restricting certain special characters.

Note that client-side scripting can be bypassed or modified by the user, so it's important to also add server-side validation to ensure that the data being submitted is valid and secure.

If you found my response helpful or applicable, please consider marking it as correct or helpful to assist others who may be seeking the same information.

---------------
Regards,
Rajesh Singh