How to write client script to validate the number input

Deepa12
Tera Contributor

i am trying write client script to check the field contains 0, 00, 000, and negative values. but the  below script working for 0. it didnt shows error msg - 00, 000.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.addInfoMessage("the new value="+newvalue);

var value = g_form.getValue('fieldname');
var regEx = /^\d+(\.\d+)*$/;
if (!regEx.test(newValue)) {

g_form.showFieldMsg('fieldname', 'Value must be greater than 0', 'error');

}

}

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Deepa12 ,
I trust you are doing great.

 

the first thing I would suggest is to modify the regular expression used in the script to include the additional cases you want to check for. For example, to check for values containing 0, 00, and 000, you could modify the regular expression to:

 

 

 

var regEx = /^([1-9]\d*|0)(\.\d+)?$/;

 

This modified regular expression will match any non-negative number that is not a pure zero, and will also match values containing a single zero, two zeros, or three zeros before the decimal point.

To check for negative values, you could modify the if statement condition as follows:

 

if (!regEx.test(newValue) || parseFloat(newValue) < 0) {

 

This will check whether the new value is not a valid non-negative number or is less than zero, and display an error message accordingly.

 

Please mark the answer correct if it was helpful.

Regards,

Amit Gujarathi

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Deepa12 ,
I trust you are doing great.

 

the first thing I would suggest is to modify the regular expression used in the script to include the additional cases you want to check for. For example, to check for values containing 0, 00, and 000, you could modify the regular expression to:

 

 

 

var regEx = /^([1-9]\d*|0)(\.\d+)?$/;

 

This modified regular expression will match any non-negative number that is not a pure zero, and will also match values containing a single zero, two zeros, or three zeros before the decimal point.

To check for negative values, you could modify the if statement condition as follows:

 

if (!regEx.test(newValue) || parseFloat(newValue) < 0) {

 

This will check whether the new value is not a valid non-negative number or is less than zero, and display an error message accordingly.

 

Please mark the answer correct if it was helpful.

Regards,

Amit Gujarathi

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi