Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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

0
Ronniel Allan Castanito
Asked:
Ronniel Allan Castanito
  • 4
  • 4
1 Solution
 
leakim971PluritechnicianCommented:
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

0
 
Ronniel Allan CastanitoIT ManagerAuthor 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.


0
 
leakim971PluritechnicianCommented:
What about a dropdown?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ronniel Allan CastanitoIT ManagerAuthor 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.
0
 
Ronniel Allan CastanitoIT ManagerAuthor 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
0
 
leakim971PluritechnicianCommented:

function IsDigit(obj)
{
a = obj.value;
if(a.length==0 || isNaN(a) || parseInt(a)!=a)
{
    alert("hey that's not a number");
    return false;
}
else if(parseInt(a)<1960 || parseInt(a)>2011)
{
    alert("hey you that's not in the range of 1960-2011");
    return false;
}
return true;
}

Open in new window

0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Thanks a lot that's done the trick.
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now