Solved

jQuery Help

Posted on 2014-03-17
6
215 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

810 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