How we can add the validation for a variable to accept only numbers and '.'(dot). Please suggest.

Archana23
Tera Contributor

How we can add the validation for a variable to accept only numbers and '.'(dot).

Please suggest.

2 ACCEPTED SOLUTIONS

Runjay Patel
Giga Sage

Hi @Archana23 ,

 

You can use catalog client script and use below script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Regular expression to allow only numbers and dots
    var regex = /^[0-9.]*$/;

    if (!regex.test(newValue)) {
        g_form.showFieldMsg('variable_name', 'Only numbers and dots are allowed.', 'error');
        g_form.setValue('variable_name', oldValue); // Revert to the old value
    } else {
        g_form.clearMessages(); // Clear any existing messages
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...

Anand Kumar P
Giga Patron

Hi @Archana23 ,

 

You can try below regex

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var pattern =  /^(([0-9.]?)*)+$/;  
    if (!pattern.test(newValue)) {
		g_form.clearValue('u_amount');
        g_form.showFieldMsg('u_amount', 'Accepted format : 354621.32', 'error', true);
        
    } 
   
}

var regex = (/^(([0-9.]?)*)+$/);

^[0-9 \.]+$g 

 If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

View solution in original post

2 REPLIES 2

Runjay Patel
Giga Sage

Hi @Archana23 ,

 

You can use catalog client script and use below script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Regular expression to allow only numbers and dots
    var regex = /^[0-9.]*$/;

    if (!regex.test(newValue)) {
        g_form.showFieldMsg('variable_name', 'Only numbers and dots are allowed.', 'error');
        g_form.setValue('variable_name', oldValue); // Revert to the old value
    } else {
        g_form.clearMessages(); // Clear any existing messages
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...

Anand Kumar P
Giga Patron

Hi @Archana23 ,

 

You can try below regex

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var pattern =  /^(([0-9.]?)*)+$/;  
    if (!pattern.test(newValue)) {
		g_form.clearValue('u_amount');
        g_form.showFieldMsg('u_amount', 'Accepted format : 354621.32', 'error', true);
        
    } 
   
}

var regex = (/^(([0-9.]?)*)+$/);

^[0-9 \.]+$g 

 If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand