Question Regular Expression Currency

Krishna101
Tera Contributor

Hi Community,
I have cretaed variable name is Amount and type as single line text. I have created Question Regular Expression for this variable to all format of $12.00.
Question Regular Expression :  ^\$[0-9]+\[,]+\.?[0-9]*$
but user want to this format only to allow $4,000,000.00
How can create Question Regular Expression to allow above format. Could you please help into this.

Krishna101_0-1730275375124.png

Thanks in Advance
Krishna

1 ACCEPTED SOLUTION

Hi @Krishna101

 

Instead of creating and using a validation regex pattern, could you please try creating an onChange client script like the one below?

 

Type: onChange

Variable Name: currency (your_variable_name)

script: 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   // Regular expression to match the format $4,000,000.00
    var regex = /^\$[1-9]{1}(?:\d{0,2}(?:,\d{3})*)?\.\d{2}$/;
    
    // Get the value of the Amount field
    var currencyValue = g_form.getValue('currency');
    
    // Check if the Amount field value matches the regex pattern
    if (!regex.test(currencyValue)) {
        // If not matched, display an error message
        g_form.showFieldMsg('currency', 'Please enter the amount in the format $4,000,000.00', 'error');
    } else {
        // Clear the field message if the value matches the pattern
        g_form.clearMessages();
    }
   
}

 

 

Here are some outputs:

ashish_parab_0-1730801194453.png

ashish_parab_1-1730801251715.png

 

If this also doesn't work, try replacing the regex pattern in the script as provided in this thread. 

Let me know if it works for you!

 

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

 

Thanks and Regards,

Ashish

View solution in original post

13 REPLIES 13

Ashish Parab
Mega Sage

Hey @Krishna101 ,

 

Use below Regular Expression:

^\$[1-9]{1}(?:\d{0,2}(?:,\d{3})*)?\.\d{2}$

 

Using the site below, you can test whether your input validates against your regular expression.

https://regexr.com/

 

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

 

Thanks and Regards,

Ashish

Hi Ashish,
Its not working

@Krishna101 , The regex pattern is working for me. Please see the screenshots below.

 

If I enter incorrect input, it doesn't accept it.

ashish_parab_0-1730288852721.png

 

When I enter input according to the field message, it is accepted.

ashish_parab_2-1730289334565.png

 

Also its accepting your given input as $4,000,000.00

ashish_parab_3-1730289488926.png

 

 

Could you please validate your input on the site I shared earlier? If it doesn’t work, could you provide additional input examples?

 

 

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

 

Thanks and Regards,

Ashish

Krishna101_0-1730293363151.png

Please find here