Trying to create a simple client script that ensures "a < field_value < b"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 10:11 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 10:29 AM - edited 04-17-2024 10:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 11:06 AM
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.
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
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 01:40 PM
Hi @C_dric Chen,
When you print out the 'type' of the value, it will show it's a string.
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
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