Trying to create a simple client script that ensures "a < field_value < b"

C_dric Chen
Tera Contributor

Hello, people.

I'm trying to create a supposedly simple client script to make sure the value of a "Decimal" field is bigger than 1000000 and smaller than 1000000000. Here are my scripts:

 

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

   //Type appropriate comment here, and begin script below
   var valueQ = g_form.getValue("field_inQuestion");

	if (valueQ < 1000000) {
		g_form.setValue("field_inQuestion", "");
		g_form.addErrorMessage("The value Q cannot be smaller than 1 million.");
	} else if (valueQ > 1000000000) {
		g_form.setValue("field_inQuestion", "");
		g_form.addErrorMessage("The value Q cannot be bigger than 1 billion.");
	}
}

 

But the script isn't working as intended. It can only detect and clear numbers up to 999, and it cannot detect any number bigger than 1000000000. Would anyone like to point out what is wrong with my scripts?

Thank you in advance!

3 REPLIES 3

Ranjit Nimbalka
Mega Sage

Hi @C_dric Chen ,

 

Not sure but why you are using g_form.setValue() you should use g_form.clearValue() function to clear value other code looks good try making this changes.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit

Community Alums
Not applicable

Hi @C_dric Chen ,

I tried your problem it works for me. Please refer below steps 

You can create a variable and set the data type to string. 

SarthakKashya2_0-1713376884722.png

Create a OnChange client script on the changes of that same field i.e, Enter Number in my case and add the below code

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

    //Type appropriate comment here, and begin script below
    alert("num = " + newValue);
    var valueQ = newValue;

    if (valueQ < '1000000') {
        // g_form.setValue("field_inQuestion", "");
        g_form.clearValue('enter_number');
        g_form.addErrorMessage("The value Q cannot be smaller than 1 million.");
        alert("The value Q cannot be smaller than 1 million.");

    } else if (valueQ > '1000000000') {
        // g_form.setValue("field_inQuestion", "");
        g_form.clearValue('enter_number');
        g_form.addErrorMessage("The value Q cannot be bigger than 1 billion.");
        alert("The value Q cannot be bigger than 1 billion.");

    }

}

 

Here's the result 

I Entered value 10000, which is smaller than 1 million 

SarthakKashya2_1-1713377133178.png

 

SarthakKashya2_2-1713377149339.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

 

 

James Chun
Kilo Patron

Hi @C_dric Chen,

 

When you print out the 'type' of the value, it will show it's a string.

JamesChun_0-1713386192677.png

 

So, to do your comparison, you will need to change its type to an integer

You can use parseInt to convert the string to an Integer but what I found out is that it won't work for Decimal type field. i.e. it will ignore the trailing zeroes and just return 1 if the value is something like 1,000,000

JamesChun_1-1713386348258.png

So, if you are intending not to use decimal points for the field, change its type to something like Long or an Integer and use parseInt to convert the string into an integer.

 

Cheers