We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

javascript

Ricky Nguyen
Ricky Nguyen asked
on
Medium Priority
233 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

Author

Commented:
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.


leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
What about a dropdown?

Author

Commented:
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.

Author

Commented:
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
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks a lot that's done the trick.
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You're welcome! Thanks for the points!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.