I'm developing app in Titanium using Javascript and trying to realize the following check logic:
User has entered one value in range between 1 and 200, then he/she should enter second value in range between 1 and the first value(less or equal, but no more then the first value).
Here is my code:
var value_alert = ''; //First value
var value_remind = ''; //Second value (should be less or equal)
var default_value_alert = 10; //Default first value for TextField
var default_value_remind = 5; //Default second value for TextField
// handle and save the first value entered by user
function doOpenAlert(){
var input_text = Ti.UI.createTextField({
keyboardType: Ti.UI.KEYBOARD_PHONE_PAD
});
if(value_alert === ''){
input_text.value = default_value_alert;
} else {
input_text.value = value_alert;
}
var dialog = Ti.UI.createOptionDialog({
title : "Specified distance in the range 1-200 km",
androidView : input_text,
buttonNames : ['Ok', 'Cancel']
});
dialog.show();
dialog.addEventListener('click', function(e){
if(value_remind === ''){
value_remind = default_value_remind;
}
if(e.index == 0){ // Check is Ok pressed
// check_number = isInt(input_text.value);
if(input_text.value >= 1 && input_text.value <= 200){ // Check that the first value is in range
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "Distance is " + input_text.value + " km."
});
toast.show();
value_alert = input_text.value; // Saving the first value entered by user
} else if(input_text.value == 0){
alert("The field is empty.");
} else if(!(input_text.value >= 1 && input_text.value <= 200)){
alert("Range is between 1 and 200 km.");
}
}
});
}
// handle and save the second value entered by user
function doOpenMinne(){
var input_text = Ti.UI.createTextField({
keyboardType: Ti.UI.KEYBOARD_PHONE_PAD
});
if(value_remind === ''){
input_text.value = default_value_remind;
} else {
input_text.value = value_remind;
}
var dialog = Ti.UI.createOptionDialog({
title : "Remind before number",
androidView : input_text,
buttonNames : ['Ok', 'Cancel']
});
dialog.show();
dialog.addEventListener('click', function(e){
if(value_alert === ''){
value_alert = default_value_alert;
}
if(e.index == 0){
// check_number = isInt(input_text.value);
if(input_text.value >= 1 && input_text.value <= value_alert){ // Check if the second value in is range between 1 and the first value
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "Remind at " + input_text.value + " km."
});
toast.show();
value_remind = input_text.value; // Saving the second value entered by user
} else if(input_text.value == 0){
alert("The field is empty");
} else if(!(input_text.value >= 1 && input_text.value <= 200)){
alert("The range is between 1 and 200 km");
}
}
});
}
For example, it works well in the following combination:
1)
First value - 10;
Second value - 5;
2) First value - 105; Second value - 101;
And the main thing, if the first value is >= 100 , but the second is < 100 - it doesn't work.
It seems that conditions are correct, but works incorrect - can't find a mistake.