javascript - multiple comma seperate value compare using JS

184

enter image description here

I want validation using a comma separated value.

Here in the image, there are two fields : one is "Saloon Price" (value :10,10,10,10), and another is "Saloon Offer Price" (value :11,11,11,11).
The first value must be lower than the second.

Saloon price Value >= Saloon Offer Price value validations based on first value of saloon price and saloon offer price same for second , 3rd ...n

var size_weight_lengh = size_weight.split(','); 
var saloon_price = validator.getFieldElements('saloon_price').val(), 
    saloon_price_lengh = saloon_price.split(','); 
var saloon_offer = validator.getFieldElements('saloon_offer_price').val(), 
    saloon_offer_lengh = saloon_offer.split(','); 

if(saloon_price_lengh.length === saloon_offer_lengh.length) { 
    for(var i=0; i<= saloon_price_lengh.length-1; i++) { 
        if((saloon_price_lengh[i]) >= (saloon_offer_lengh[i])) { 
            return true; 
        } 
        return false; 
    } 
 }
833

Answer

Solution:

Split the string and then do a value comparison of two array elements. It uses "break" and "continue" to reduce the unnecessary iterations over the loop. Here is the full script. Adjust the functionality accordingly.

    $(document).ready(function () {
        var value = ComparePrice(); 
        alert(value);
    });

    function ComparePrice() {
    var salonOfferPrice = $('#saloon_offer_price').val();
    var salonPrice = $('#saloon_price').val();
    var offerPriceArray = salonOfferPrice.split(",");
    var priceArray = salonPrice.split(",");
    var isValid = false;
    if (offerPriceArray.length == priceArray.length) {
        for (var i = 0; i < offerPriceArray.length; i++) {
            for (var j = 0; j < priceArray.length; j++) {
                if (i == j) {
                    if (offerPriceArray[i] < priceArray[j]) {
                        alert(offerPriceArray[i] + "is less than" + priceArray[j]);
                        isValid = true;
                    }
                    else {
                        alert(offerPriceArray[i] + "is greater than or equal" + priceArray[j]);
                        return false;
                    }
                }
                else {
                    continue;
                }

            }
        }
    }
    return isValid;
}
14

Answer

Solution:

You have to do value by value comparison.

    var sp="10,20,30"; //get your field values here
    var sop="5,10,15";

    var spArr = sp.split(','); //split the values using comma
    var sopArr = sop.split(',');

    if(spArr.length === sopArr.length){
       for(var i in spArr){
         if(parseInt(spArr[i])<parseInt(sopArr[i])){
              //throw some error or your logic goes here.
         }
       }
    }

Just make sure that you accept only numbers and comma using some regex check in the text field.

People are also looking for solutions to the problem: php - How do I write a function for showing only one category?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.