Search code examples
javascripttitaniumappcelerator

Condition for a number to be in range - Titanium/Javascript


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.


Solution

  • I think that the issue you're having is that you're comparing the values as strings rather than numbers. When you compare two strings, Javascript bases the comparison on the Unicode values of the characters in order. What does that mean for you? The short answer is, that while "90" < 200 is true because that comparison results in the "90" being coerced to 90, "90" < "200" is false because "9" is greater than "2".

    In order to avoid this behavior, you need to convert one or both of your variables to numbers. This answer on converting strings into numbers shows a number of ways for you to do that, but in your case, I think that parseInt(input_text.value, 10) <= parseInt(value_alert, 10) would work fine for you.