Solved

jQuery Help

Posted on 2014-03-17
6
216 Views
Last Modified: 2014-03-22
This is my current code, the quantity11 field has to have an input that is greater than 1 for the form to submit.  I have two other fields I need checked as well quantity12 and quantityreg.

<script type="text/javascript">
		function check() {
			var elements = document.getElementsByName("quantity11");


			for (var i=0; i<elements.length; i++) {

				var nmbr = parseInt(elements[i].value,10);
				if (nmbr < 1){
					alert("Please add at least 1 treat pack to your order to proceed.");
					return false;
				}
			}
			
			return true;
		}
	</script>

Open in new window


 I need to add quantity11, quantity12, and quantityreg all together and then check to see if they are greater than 1 to return true.  How can I add them together on the 3rd line?
0
Comment
Question by:N R
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 39934504
I don't know. I think you should consider using a JQuery validator. There are probably more than one available, but after a quick search here's one that looks promising:

http://jqueryvalidation.org/

Simply use as this example provides:
http://jqueryvalidation.org/min-method
0
 
LVL 11

Author Comment

by:N R
ID: 39934541
That method would work but I still need the 3 fields sum'd or added together before the check somehow.
0
 
LVL 11

Author Comment

by:N R
ID: 39934554
I tried this approach but the code doesn't appear to work.
<script type="text/javascript">
		function check() {
			var element1 = document.getElementsByName("quantity11");
			var element2 = document.getElementsByName("quantity12");
			var element3 = document.getElementsByName("quantityreg");
			var elements = +element1 + +element2 + +element3;
			for (var i=0; i<elements.length; i++) {

				var nmbr = parseInt(elements[i].value,10);
				if (nmbr < 1){
					alert("Please add at least 1 treat pack to your order to proceed.");
					return false;
				}
			}
			
			return true;
		}
	</script>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 11

Accepted Solution

by:
N R earned 0 total points
ID: 39934599
Just fixed the issue on my own with the following code.

<script type="text/javascript">
		function check() {
			var element1 = document.getElementsByName("quantity11");
			var element2 = document.getElementsByName("quantity12");
			var element3 = document.getElementsByName("quantityreg");
			
			for (var i=0; i<element1.length; i++) {

				var nmbr1 = parseInt(element1[i].value,10);
				var nmbr2 = parseInt(element2[i].value,10);
				var nmbr3 = parseInt(element3[i].value,10);
				var nmbr = nmbr1 + nmbr2 + nmbr3;
				if (nmbr < 1){
					alert("Please add at least 1 treat pack to your order to proceed.");
					return false;
				}
			}
			
			return true;
		}
	</script>

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 39934830
Fine, but if nbr1 = -5 and nbr2 = 10 and nbr3 = 10, you have a nbr = 15 and you didn't meet your requirements.
0
 
LVL 11

Author Closing Comment

by:N R
ID: 39947252
Found the solution on my own with trial and error.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question