Restrict Positive values in catalog client script

lakshmidurga
Tera Expert

Hi All,

I need to restrict a field to enter only positive values.

I have written the below client script,but the field is allowing values starting with '-', for example -22.

function onChange(control, oldValue, newValue, isLoading) {

    if (!isLoading) {

          if (newValue != '') {

                //get the integer value of the field

                var v_cost_center = g_form.getIntValue('software_maintenance_cost');

                //check to see if there are any non numeric characters in the string

                if ((isNaN(newValue) == true)|| (nbrwn < 0) || isNumber(newValue)<0)

  {

                      alert("Please enter Numeric Values.");

                      g_form.setValue('software_maintenance_cost', '');

                }

          }

    }

}

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Here we go.



check the below script. i used regex to restrict the -ve number.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }


  var specialCharRegex =/^\d{0,10}(\.\d{0,2})?$/g;


  if(!specialCharRegex.test(newValue)){


  alert('Please do not enter -ve value');


  g_form.setValue('description','');


  }


}



hope it will help you.



Thanks,


Harshvardhan


View solution in original post

5 REPLIES 5

Hi Harsh,



Thanks for your reply.



Its working