- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2020 10:29 AM
We have a Catalog Client Script that we are applying to the OnChange event of a Single Line Text variable. We only want to allow numeric entries (it is a price field). The script sucessfully only allows numeric entries. The code looks like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var integer = /^[0-9.,]*$/;
if(!integer.test(newValue))
{
alert('Please enter a valid number.');
g_form.clearValue('settlement_amount');
}
}
We would like to enhance this a little to only allow positive entries (entries greater than zero).
How can we amend this code to do that?
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2020 05:49 AM
OK, I was overthinking things. The first clause already does not allow negative entries. So all you really have to do is add a second IF clause to check for a second condition to see if the value entered is zero.
So, my amended code looks like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//make sure they only enter numbers, comma, decimal, or dollar sign
var integer = /^[0-9.,]*$/;
if(!integer.test(newValue))
{
alert('Please enter a valid number.');
g_form.clearValue('settlement_amount');
}
//make sure they do not enter just zero
if(newValue == 0)
{
alert('Please enter a non-zero value.');
g_form.clearValue('settlement_amount');
}
}
This works great, as it allows any positive number greater than zero, including decimals, and allows commas in the numeric entries.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2020 11:23 AM
OK, so I see your updated code above, and I tried that. Unfortunately, it fixed one thing, but broke something else.
The original code allowed for commas and decimals. But also allowed zero entries, and we only allowed for entries greater than zero.
Your new code does not allow zero entries, but also does not allow any commas or decimals. We can live without the commas, but we must allow decimals, as not all entries will be whole dollar amounts.
So we still are not quite there yet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2020 05:49 AM
OK, I was overthinking things. The first clause already does not allow negative entries. So all you really have to do is add a second IF clause to check for a second condition to see if the value entered is zero.
So, my amended code looks like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//make sure they only enter numbers, comma, decimal, or dollar sign
var integer = /^[0-9.,]*$/;
if(!integer.test(newValue))
{
alert('Please enter a valid number.');
g_form.clearValue('settlement_amount');
}
//make sure they do not enter just zero
if(newValue == 0)
{
alert('Please enter a non-zero value.');
g_form.clearValue('settlement_amount');
}
}
This works great, as it allows any positive number greater than zero, including decimals, and allows commas in the numeric entries.