You are viewing limited content. For full access, please sign in.

Question

Question

Currency field validation that number is a minimum of 0 and is a multiple of 1000

asked on October 25, 2021

I have a currency field on a form and I'd like it to be validated to ensure that the value entered is a multiple of 1000 but still allow a zero to be entered.

I'm assuming this would need to be javascript, so if anyone has a sample that would be great.  Thank you.

 

0 0

Answer

SELECTED ANSWER
replied on October 26, 2021 Show version history

Hi Mike

This is the way I go about it, using either a Number field or a Single Line field and prepend a $ to them to look like a Currency field. This way I can use the native functions of the field that best suits my needs, either a Min-Max or Regex to control the input.

Using a Number or Single Line field. Add a class (ie: moneyclass) to the field

Use one of the following JS to prepend a $ to the front of the field to appear as a Currency field

For a number field

$(document).ready(function() {

  $('.moneyclass[attrtype="number"] .cf-field').prepend('<span class="margin-end">$</span>');

}); 

 

For a textfield field

$(document).ready(function() {

  $('.moneyclass .cf-field').prepend('<span class="margin-end">$</span>');

}); 

1 0
replied on October 26, 2021 Show version history

Steve,

Thanks for the tip.  How would I use a Regex to make sure that the value input in the field is a multiple of 1000?  The user should be able to enter any value, 0 or higher,  in multiples of 1000. So, 0 is a valid value and any number above 0 is valid as long as it is a multiple of 1000.

Thank you.

 

0 0
replied on October 26, 2021 Show version history

This would allow a number from 1,000 up to $999,999,000 in thousands or 0

(\d{1,6}?000$)

0 0
replied on October 26, 2021

Would the Regex go into the Error Messages?  I tried creating one using the following options:

When = Regular expression does not match

For = These individual fields

But the drop-down list of fields that was provided did not include my new number field. 

0 0
replied on October 26, 2021

Never mind...I found that the regex is in single line fields but not in number fields, so I'll use a single line field.  Thanks.

0 0
replied on October 26, 2021

The regex doesn't seem to work if I enter a value such as 2500, but I found the following that seems to work as expected:

^[1-9]+[0-9]*000$

Now that I'm using a single line text field, is there a way to format the number with a thousands separator comma?

0 0
replied on October 26, 2021 Show version history

As far as the Error Message, this worked for me

 

1 0
replied on October 26, 2021 Show version history

You'd have to use JS to apply a mask to the field for that.

Just search for Mask here on answers and you'll see lots of examples

You'd also have to account for those comma's in your regex as well

1 0
replied on October 26, 2021

Thanks Steve...using a combination of your suggestions and the regex I found elsewhere and got a good working solution.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.