How to force inputting 2 decimal places for a number field in client scripting?

Si Min
Giga Expert

Hi,

I have a field which created under single line text and having below client script to only accept numbers plus a period to indicate as price.

How do I round the value with 2 decimal point? And restrict them to only inputting 2 decimal point if user chooses to enter decimal values instead of rounding figures.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (isLoading || newValue === '') {
		
		return;
		
	}
	
	var reg = /^[1-9]\d*(\.\d+)?$/;
	
	var ans = g_form.getValue('estimate_price_sgd');
	
	if(!reg.test(ans))
		
	{
		
		alert('Please enter a valid price under SGD currency.');
		g_form.clearValue('estimate_price_sgd');
		
	}
	
}

 

Thanks in advanced!

1 ACCEPTED SOLUTION

Hi there,

Because your image shows a Variable, you are talking about a Catalog Client Script? And not Client Script?
Is that correct?

If so:
No need to go for scripting.

Check out the functionality which sinds Madrid is available:
(I see version Madrid in your post)
https://community.servicenow.com/community?id=community_article&sys_id=f5b8a988db057300d82ffb2439961...

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So user should enter at max 2 values after the decimal and you want to round off as well

the above regex you shared doesn't seem to work

try this

var reg = /^[0-9]*\.[0-9]{2}$/;

do you want them to restrict to 2 decimals then use above

OR

allow then to give as many decimal values and round off from script; this will round off to decimals places

var roundValue = parseFloat(newValue).toFixed(2);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

Is it something like below?

I still can't get it right and yes allow them to give any values but to round off to 2 decimal places.

 

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

	var ans = g_form.getValue('estimate_price_sgd');
	
	var roundValue = parseFloat(newValue).toFixed(2);
		
	if(!reg.test(ans))
		
	{
		
		alert('Please enter a valid price under SGD currency.');
		g_form.clearValue('estimate_price_sgd');
		
	}
}

Thanks.

Hi,

If you are allowing them to give any decimals and you will be rounding off then no need to give the message I believe

this you can do then in before insert/update business rule as well

Let them give any decimal places value and in your BR script you round it off to 2 decimal places.

Will that work?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 

can you please share the script , if the user should enter less than 3 after decimal it should accept after decimal.

 

0.1 or 0.22 or 0.333 it should accept, if they enter more than 0.4444 it should not accept user.