The CreatorCon Call for Content is officially open! Get started here.

Related to Numeric variable validation on service portal

Are Kaveri
Tera Contributor

Hello,

 

the below is my requirement.

for one of our clients there is a concern related to one variable.

cost variable for which we are validating its a numeric or not.

they want cost= 20,000.

not cost = 20000.

 

How to validate below scenario.

 

i have written below onchange script for cost to check whether it is numeric or not.

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

    var decimalRegex = /^\d*(\.\d{1,2})?$/;


    g_form.hideFieldMsg('cost', true);

    if (!decimalRegex.test(g_form.getValue('cost'))) {

        g_form.showFieldMsg('cost', 'Cost must be a numeric or 2 places decimal value.', 'error');
    }

  

 

How to check the comma separated values in the script ?

 

 

1 ACCEPTED SOLUTION

Hi @Are Kaveri,

 

It sounded like you only wanted validation of the entered format. Not that it changes it automatically...

 

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

      // remove any commas from earlier formatting
      var value = newValue.replace(/,/g, '');
      // try to convert to an integer
      var parsed = parseInt(value);
      // check if the integer conversion worked and matches the expected value
      if (!isNaN(parsed) && parsed == value) {
        // update the value
        g_form.setValue('cost', new Intl.NumberFormat('en-US').format(value));
      }  
}

 

 

This should work 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

6 REPLIES 6

Peter Bodelier
Giga Sage

Hi @Are Kaveri 

 

You can use this regex for your validation.

 

/^\d{1,3}(,\d{3})*(\.\d+)?$/g

 

 

I would recommend to use the OOTB Regex validation, instead of writing your own client script.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

@nowgpt1 @Peter Bodelier  i have tried both the regex but none of them were working .  It was showing error message if i give more than 4 digits.

@Are Kaveri 

 

That is what you where asking for right?

20000 not allowed

20,000 allowed


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

@Peter Bodelier  i am entering 2000 but it should automatically take comma after 3 digits right. That is not happening it was throwing the error message.