Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
Hi everyone,
Having some trouble with a simple function. Doing a simple calculation function with input fields for a variable a and b and the function is supposed to return a x b.
Having trouble though with if someone writes something else then a number in the input fields like a symbol or a letter e.g. @,f,g,% or some thing like that. Tried different variations and here is the idea of the code:
$scope.calculate = function(a,b) {
a = $scope.app.view.Home.wdg["textInput-1"].text;
b = $scope.app.view.Home.wdg["textInput-2"].text;
var q;
q = a * b;
if (typeof q !== 'number') {
$scope.app.view.Home.wdg["label-2"].text = "Write numbers";
}
else{
$scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;
}
}
The problem is the if statement, the idea is that if q is a datatype anything under then a number the text label should say write numbers, else it should display the value of q.
But I try this in different variations and all I get is either the value of q or if you write symbols it display "NaN".
Someone know of to resolve this?
Solved! Go to Solution.
Hello,
thanks for answer, tested that functionality as well but didn't really work well. It worked in preview and limited in such a way so only numbers could be written in the text field, but when publishing and used in phone it didn't work well.
Have asked around and searched the web and eventually ended up with this code that worked:
$scope.calculate = function(a,b) {
a = $scope.app.view.Home.wdg["textInput-1"].text;
b = $scope.app.view.Home.wdg["textInput-2"].text;
var q;
q = a / b;
if (Number.isFinite(q)){
q = q.toFixed(2);
$scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;
}
else{
$scope.app.view.Home.wdg["label-2"].text = "Write integers";
}
}
Hi @PhilipB ,
I think NaN means that value is undefined, and you can check first the value if it is undefined.
You can see e.g. this post (How to check a not-defined variable in JavaScript)
but I think , when I look on your code - is the quesiton what should haepend when you try to multiply some undefined things. May be some exception should be thrown and you can catch it in this case.
May be in this case you can check fist the text value of the input widget / what this string contains : if it is numeric and could be parse to a number e.g. parseInt(), parseFload or Number() or is only string and you need to use any sting operations.
For Input widgets you can specify the type as Number with max and minimum value - so this will ensure that value could not be any other text.
Hello,
thanks for answer, tested that functionality as well but didn't really work well. It worked in preview and limited in such a way so only numbers could be written in the text field, but when publishing and used in phone it didn't work well.
Have asked around and searched the web and eventually ended up with this code that worked:
$scope.calculate = function(a,b) {
a = $scope.app.view.Home.wdg["textInput-1"].text;
b = $scope.app.view.Home.wdg["textInput-2"].text;
var q;
q = a / b;
if (Number.isFinite(q)){
q = q.toFixed(2);
$scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;
}
else{
$scope.app.view.Home.wdg["label-2"].text = "Write integers";
}
}