Multiple Comma Seperate Value Compare Using Js
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
Solution 1:
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);
});
functionComparePrice() {
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]);
returnfalse;
}
}
else {
continue;
}
}
}
}
return isValid;
}
Solution 2:
You have to do value by value comparison.
var sp="10,20,30"; //get your field values herevar sop="5,10,15";
var spArr = sp.split(','); //split the values using commavar 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.
Post a Comment for "Multiple Comma Seperate Value Compare Using Js"