Solved

jQuery Help

Posted on 2014-03-17
6
213 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now