Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
I have used a validor to check textfields text are empty or not and if not empty then i am executing a service because validator will return TRUE otherwise i show message with status message FUNCTION to show that please enter the text into fields but it is not working. if i normally write output= true or false then only it works. can anyone please guide me in this regard. I have put a screenshot of bindings and my parameters also please have a look into it. I am using Thingworx 9.3
let Output = true; // Initialize Output as true
// The isValid function should accept a single parameter to check.
const isValid = (param) => {
return param !== null && param!=="" && param.trim() !== '';
};
try{
// Check each parameter individually
if (!isValid(emailAddress)) {
console.log('Invalid email address');
Output = false;
}
if (!isValid(firstName)) {
console.log('Invalid first name');
Output = false;
}
if (!isValid(lastName)) {
console.log('Invalid last name');
Output = false; // Change from return false to setting Output to false
}
if (!isValid(loginName)) {
console.log('Invalid login name');
Output = false;
}
if (!isValid(loginPassword)) {
console.log('Invalid login password');
Output = false;
} } catch (error) {
console.log('Error occurred: ' + error.message);
Output = false;
}
console.log("Output: " + Output);
Solved! Go to Solution.
What browser are you using? Because I stumbled upon a strange behavior a few years ago:
Output = false;
if (inputStr) {
Output = true;
}
This snippet will always return "true" if you are using a Chrome-based Browser; no matter what the "inputStr" is. When executing the same expression in Firefox or IE, it will work fine.
I have contacted the PTC support about this. The answer was, that this was not a Thingworx bug since expression are executing by the browsers JS engine, therefore the results may differ.
So you have to use the good ol' if-else pattern:
if (inputStr) {
Output = true;
} else {
Output = false;
}
One additional hint:
If you checking strings for falsy values, it is sufficient to use "if (!myString)" as check. So you don't need the check for undefined/null/"". Be careful when using it for numeric data types since 0 (zero) is also a falsy value.
Hi @MA8731174 ,
In validator function, Hope this below attached code will helps in your scenario.
IP_TextBox1 = IP_TextBox1.trim();
IP_TextBox2 = IP_TextBox2.trim();
IP_TextBox3 = IP_TextBox3.trim();
/* INITIAL VALIDATION CHECK */
let validationMesg = '';
if (IP_TextBox1 == null || IP_TextBox1 == undefined || IP_TextBox1 == "") {
validationMesg = 'invalid';
TW.log.info('Please Enter Value in IP_TextBox1');
} else if (IP_TextBox2 == null || IP_TextBox2 == undefined || IP_TextBox2 == "") {
validationMesg = 'invalid';
TW.log.info('Please Enter Value in IP_TextBox2');
} else if (IP_TextBox3 == null || IP_TextBox3 == undefined || IP_TextBox3 == "") {
validationMesg = 'invalid';
TW.log.info('Please Enter Value in IP_TextBox3');
} else {
validationMesg = 'valid';
}
/* FINAL OUTPUT PRINT */
if (validationMesg == 'valid') {
Output = false;
} else {
Output = true;
}
Also, Kindly find the link for Supported Runtime Functions and Objects.
Thanks & Regards,
Arun C
Put some console.log into the isValid method with input and the result. I guess this should help you finding out if it behaves like you expect.
You can also reference the "Use truthy and falsy cases" in Best Practices for Coding in JavaScript which could simplify your isValid method to
return !!param;
as JS evaluates a lot of cases automatically to false (like undefined, null, "", ...).
Also I'm not sure if maybe the 'let Output = false' is an issue in the 1st line. Maybe try with 'Output =false' instead - as the 'let' keyword defines the variable only for the current scope - so it may not exist anymore in the internal TWX logic.
What browser are you using? Because I stumbled upon a strange behavior a few years ago:
Output = false;
if (inputStr) {
Output = true;
}
This snippet will always return "true" if you are using a Chrome-based Browser; no matter what the "inputStr" is. When executing the same expression in Firefox or IE, it will work fine.
I have contacted the PTC support about this. The answer was, that this was not a Thingworx bug since expression are executing by the browsers JS engine, therefore the results may differ.
So you have to use the good ol' if-else pattern:
if (inputStr) {
Output = true;
} else {
Output = false;
}
One additional hint:
If you checking strings for falsy values, it is sufficient to use "if (!myString)" as check. So you don't need the check for undefined/null/"". Be careful when using it for numeric data types since 0 (zero) is also a falsy value.