Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery Help

Posted on 2014-03-17
6
Medium Priority
?
226 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 12

Author Comment

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

Author Comment

by:Nathan Riley
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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 12

Accepted Solution

by:
Nathan Riley 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 12

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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?
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
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 how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

609 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