Condition in client script if price field == 0

Nico12
Mega Sage

Hi,

 

In a UI action, i need to check if a price field is > 0,01 Euros before allow user to submit the form.

 

Here is what the field looks like :

 

Capture d’écran 2023-04-25 à 18.49.54.png

Here is the alert :

 

Capture d’écran 2023-04-25 à 18.50.15.png

 

What is the best way to compare price field ?

 

Regards,

2 REPLIES 2

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Nico12 ,

 

You should obtain the floating point value using the javascript function parseFloat like this and then compare it

var rate = parseFloat(current.base_rate);

 

Please refer to this doc as well: https://docs.servicenow.com/en-US/bundle/utah-platform-administration/page/administer/currency/conce...

 

If my answer has helped with your question, please mark it as correct and accepted solution

Thanks!!

 

Rahul Kumar17
Tera Guru

Hi,

You can compare the value of the price field to 0.01 using a conditional statement in your UI action.

Here's an example of what the script could look like:

var price = parseFloat(g_form.getValue('price_field'));

if (price < 0.01) {
  alert('Price must be greater than 0.01 Euros.');
  return false; // prevent the form from being submitted
} else {
  return true; // allow the form to be submitted
}

 

Thanks,

Rahul Kumar

 

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar