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

Here in this Video, I have covered the Custom Application Pattern Troubleshooting and configuration Thank you for visiting my channel. Here, I'll share various technical knowledge. Feel free to reach out to me directly for any Service Now-related queries. Your support encourages me to consistently

Anand Kumar P
Tera 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

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

Here in this Video, I have covered the Custom Application Pattern Troubleshooting and configuration Thank you for visiting my channel. Here, I'll share various technical knowledge. Feel free to reach out to me directly for any Service Now-related queries. Your support encourages me to consistently

Anand Kumar P
Tera 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