Solved

jQuery Help

Posted on 2014-03-17
6
212 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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 11

Accepted Solution

by:
N R earned 0 total points
Comment Utility
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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
Found the solution on my own with trial and error.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now