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

Lukasz Bojara
Kilo Sage

Hi,

 

Just a thougth, why using a text filed for a price (maybe you have a reason for it)? It is better to use type Decimal or Currency, this way you will not have to worry about the value validation, e.g. Decimal filed is a number with up to two digits after the decimal points (for example, 12.34), or Currency that can take up to 4 decimal places but you can switch is off in sys_properties to limit just to 2 (it is a default setting). With those field types, you do not have to write additional code to ensure that a valid number was given.

 

Best regards,
Łukasz

Hi Lukasz,

Thanks for the reply, but in the variable types I do not see there's a 'Curreny' or 'Decimal' from the selection to choose.

find_real_file.png

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

Hi Mark,

Great help!

This is exactly what I am looking for.

Thanks!