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

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.

Anand Kumar P
Giga Patron

Hi @Are Kaveri ,

Use below script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var costValue = g_form.getValue('cost').replace(/,/g, '');

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

    g_form.hideFieldMsg('cost', true);

    if (!decimalRegex.test(costValue)) {
        g_form.showFieldMsg('cost', 'Cost must be a numeric or 2 places decimal value.', 'error');
        g_form.setValue('cost', oldValue);
    }
}

 Thanks,

Anand