I creating a web application, where I need to get a range of numbers as input:
Ex: 1-10, 0-100 etc.
The following is the code I use to get the range into variable “s” and display it. And #ucode is the ID of the textbox I use to get the input.
//JavaScript
function validate() {
var i = $("#ucode").val(); //input
i = i.replace(/ /,'');
var constraint = /([0-9]+[,-])?[0-9]+$/;
if(!constraint.test(i))
{
alert("Invalid Expression");
return;
}
var a = i.split(","); //split and array
var s = ""; //output string
if(a != "")
{
for(n = 0; n < a.length; ++n )
{
var conh = /^[0-9]-[0-9]$/;
var conc = /^[0-9]+$/;
if(conh.test(a[n]))
{
var range = a[n].split("-");
var ll = parseInt(range[0]); //lower limit
var ul = parseInt(range[1]); //upper limit
if(ul < ll) { alert("Invalid Expression"); return; }
for(m = ll; m <= ul; ++m)
s += " " + m;
}
}
alert(s); // Alert output
}
}
The problem is this function works for range 1-9 not beyond that
if I give 1-9, it displays
1 2 3 4 5 6 7 8 9
if i give 1-2,5-9, it displays
1 2 5 6 7 8 9
But if I give beyond 9 i.e. 1-10 or 5-45 it shows a blank Alert Message.
Where am I wrong? Please Help Me
Thanks in advance
First I thank @NullPointer for Idea, and @Jeff to where to use it.
The problem was at the
if()condition. So I changed the code as follows and it works fine.Now it works fine. Thanks friends.