itsm

karthik65
Tera Guru

Hi All,

I have a field for frequency in catalog item to update it in Custom Table.

This field should be 234.234 format. how can i restrict it in Client Script. 

Below is my Client Script.

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

   //Type appropriate comment here, and begin script below

   var reg = /^[0-9]*\.[0-9]\d{3}$/;
    var ans = g_form.getValue('frequency_number_u');
   
    if(!reg.test(ans))
    {
        alert('Please enter a valid value.');
        g_form.clearValue('frequency_number_u');
    }
 
}
1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @karthik65 ,

 

Use below script, i have tested it, working fine. it will accept 3 digit before and after decimal. If you before digit can be anything then use :var reg = /^\d+\.\d{3}$/;

var reg = /^\d{3}\.\d{3}$/;
    var ans = g_form.getValue('frequency_number_u');
   
    if(!reg.test(ans))
    {
        alert('Please enter a valid value.');
        g_form.clearValue('frequency_number_u');
    }

 

 

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

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

Part 1. This video serves as the introductory episode of the ServiceNow Fundamental Course playlist. Here, I've outlined the course content that I'll be delving into throughout this series. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on ...
5 REPLIES 5

raj chavan
Tera Guru
Tera Guru

Try this ...

// Regular expression to match the format "123.456"
var reg = /^\d+\.\d{3}$/;
var ans = g_form.getValue('frequency_number_u');

If my answer helped you in any way, then mark it as helpful or correct. This will help others finding a solution.

 

Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj

Runjay Patel
Giga Sage

Hi @karthik65 ,

 

Use below script, i have tested it, working fine. it will accept 3 digit before and after decimal. If you before digit can be anything then use :var reg = /^\d+\.\d{3}$/;

var reg = /^\d{3}\.\d{3}$/;
    var ans = g_form.getValue('frequency_number_u');
   
    if(!reg.test(ans))
    {
        alert('Please enter a valid value.');
        g_form.clearValue('frequency_number_u');
    }

 

 

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

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

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

Part 1. This video serves as the introductory episode of the ServiceNow Fundamental Course playlist. Here, I've outlined the course content that I'll be delving into throughout this series. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on ...

Simon Christens
Kilo Sage

If you want exactly 3 degits on either side of the . (dot)
Then you need to reuse the d{3}
Like: 

/^\d{3}.\d{3}$/

Ashish Parab
Mega Sage
Mega Sage

@karthik65 ,

Please try by using below regex:

/^\d{3}\.\d{3}$/

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish