Link to home
Start Free TrialLog in
Avatar of Ricky Nguyen
Ricky NguyenFlag for Australia

asked on

javascript

Hi Experts.

I'm currently stuck on a problem and not too sure on how to approach it so here I am.

Currently what I'm trying to do is the following:

I want a user to enter a year between the range of 1960 to 2011 anything lower than 1960 or higher than 2011 would prompt an error message. I understand that some sort of for loop is probably needed but I'm unsure of how I should go about it.

Hoping for some help :)

Thanks in advance.

Rick

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Check this :


<html>
<head>
<script>
	function fillListBox(start, end, element) {
		document.getElementById(element).length = 0;
		for(i=start;i<=end;i++) {
			opt = i-start;
			document.getElementById(element).options[opt] = new Option(i,i);
		}
	}
</script>
</head>
<body onLoad="fillListBox(1960, (new Date()).getFullYear(),'G1');"">
<select id="G1"></select>
</body>
</html>

Open in new window

Avatar of Ricky Nguyen

ASKER

Sorry I don't think I was clear

What I'm trying to do is get a function running that won't let a user enter a 4 digit number below the value 1960 or a 4 digit number above the value of 2011. This function would be applied to a form to ensure that the user enters between 1960 - 2011 but also to ensure that all entries are of numerical value.

Something like:

script, but with a for loop for the 1960-2011 or something like that
function IsDigit(obj)
{
   		var sText=obj.value;
   		var ValidChars = "0123456789";
   		var IsNumber=true;
   		var Char;

   			for (i = 0; i < sText.length && IsNumber == true; i++) 
      		{ 
      		Char = sText.charAt(i); 
      				if (ValidChars.indexOf(Char) == -1) 
         			{
					IsNumber = false;
         			}
      		}

  							if(IsNumber==true)
   							obj.className="TextBox";
					else
					{
					alert('Only numeric values are allowed, Please only enter digits 0-9');
					obj.className="error_highlight";
					obj.focus();
					}
   return IsNumber;

Open in new window


code where you call the function
Model:<br />
    Year of Manufacture:<br />
            <input type="text" name="Year" id="YearMan" maxlength="4"onblur="return IsDigit(this);" /><br  /><br /
       

Open in new window


I hope this clarifies my question.


What about a dropdown?
addition example:

If the user was to enter say the value 1 than the user would be prompted with:

"hey you that's not in the range of 1960-2011"

However if the user was to enter the value a than he would be prompted with:

"hey that's not a number"

something along those lines.
That would be the practical work around but I'm trying to figure out how to do it this way, it would also avoid the massive drop down that would be required to have each year from 1960-2011
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot that's done the trick.
You're welcome! Thanks for the points!