I have tried to correct the jQuery code below. In the ESNList text field, multiple values need to be entered separated with a comma, yet they have to be in the same range. For example, in the condition below in the loop, if you input a number in ESNList between 986329 and 999999, the second input in the ESNList field should be in the same range as the first input and so on otherwise I throw an alert saying that both ESNs should be in the same range. I did a loop to perform the check but it’s not working. Can someone demonstrate to me where the error is? A fiddle would help tons, I have a hard time sometimes understanding what people try to say on here without good demonstration of where I went wrong. Below is my code:
<html>
<head>
<script type="text/javascript" src="jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function () {
$(":text").css("border", "2px solid red");
$(":text").keyup(function(){
var enteredData = $(this).val()
console.log(enteredData);
if (enteredData == "") {
$(this).css("border", "2px solid red");
} else {
$(this).css("border", "inherit");
}
if ($(this).attr("id") == "ESNList"){
esnList = enteredData.split(',');
}
for(var i = 0; i < esnList.length; i++) {
if ( parseInt(esnList[i]) >= 986329 && parseInt(esnList[i]) <= 999999) {
$("#ddl_StxName").val("stx2");
$("#ddl_rtumodel").val("globalstar");
}
else if ( parseInt(esnList[i]) >= 660000 && parseInt(esnList[i]) <= 699999) {
$("#ddl_StxName").val("mmt");
$("#ddl_rtumodel").val("globalstar");
}
else if ( parseInt(esnList[i]) >= 200000 && parseInt(esnList[i]) <= 299999) {
$("#ddl_StxName").val("stm3");
$("#ddl_rtumodel").val("stmcomtech");
}
else if ( parseInt(esnList[i]) >= 1202114 && parseInt(esnList[i]) <= 1299999) {
$("#ddl_StxName").val("smartone");
$("#ddl_rtumodel").val("globalstar");
}
else { alert("ESNs should be within the same range"); }
}
});
});
</script> </head>
<body>
<form id="provision">
ESNList: <input type="text" id="ESNList" name="ESNList" size="30" /> <br />
ESN Start:<input type="text" id="ESNStart" name="ESNStart" size="10" /> <br />
ESN End: <input type="text" id="ESNStart" name="ESNStart" size="10" /> <br />
UnitName:<input type="text" id="STxName" name="STxName" size="30" /> <br />
Unit Model: <select name="STxName" id="ddl_StxName">
<option value="stx2">STX2</option>
<option value="stm3" selected>STM3</option>
<option value="acutec">Acutec</option>
<option value="trackpack">Trackpack</option>
<option value="mmt">MMT</option>
<option value="smartone">Smartone</option>
<option value="smartoneb" >SmartOneB</option>
</select> <br />
RTU Model Type:
<select name="rtumodel" id ="ddl_rtumodel">
<option value="globalstar">GlobalStar</option>
<option value="both">Both</option>
<option value="comtech">Comtech</option>
<option value="stmcomtech">STMComtech</option>
</select> <br />
<input type="submit" value ="submit" />
</form>
</body>
</html>
try this for debugging:
before the
for(...)line, putthen after the
for(...)line:these will be written to the javascript console, which you can view in the developer tools of most browsers. Include similar logging in your if/else statements, and you can narrow down where the problem is yourself by seeing which of these is triggered.
You’re on the right track with the
alert()you use, just use that orconsole.log()more often.