Solved

Assistance with JavaScript/jQuery validation

Posted on 2014-07-18
5
274 Views
Last Modified: 2014-07-18
I have a button called "letsgo". When it is clicked I want to make sure that both a text area (Notes) and a a text field (theperson) are populated. Could someone help me with the "if" part of my validation below? Thank you.

	window.onload = function() {
    document.getElementById("letsgo").onclick = function() {
        var TextArea = document.getElementsByName("Notes")[0];
        var TextField = document.getElementsByName("theperson")[0];
        if ..................
        {
            alert("Please enter a Person and Notes."); 
            return false;
        }
        return true;
    }
}

Open in new window

0
Comment
Question by:earwig75
  • 2
  • 2
5 Comments
 
LVL 21

Assisted Solution

by:Randy Poole
Randy Poole earned 250 total points
ID: 40204765
if (TextArea.value.length==0 && TextArea.value.length==0)
0
 

Author Comment

by:earwig75
ID: 40204804
I ended up using the below. I needed both required and 1 text area and 1 text field.

Thank you for the assistance.

if (TextArea.value.length==0 || TextArea.value.length==0)
0
 
LVL 2

Accepted Solution

by:
c l earned 250 total points
ID: 40204805
here is a jquery solution:

         $('#letsgo).bind('click', function (event) {
                document.getElementById("letsgo").onclick = function() {
                if( $('#Notes').val() == '' ) {
                    alert("Please enter a value for the Notes Field."); 
                    return false;
               } else if ( $('#theperson).val() == '' ) {
                   alert("Please enter a value for the Person Field."); 
                   return false;
              } else
                    return true;
    }
}

Open in new window


this assimes all of your form elements have IDs set to them that is the same as their names
0
 

Author Comment

by:earwig75
ID: 40204816
I've requested that this question be closed as follows:

Accepted answer: 0 points for earwig75's comment #a40204804
Assisted answer: 500 points for Randy Poole's comment #a40204765

for the following reason:

The assistance had what I believe was a typo. I also needed both to be required.
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40204807
ahh ok, but you are comparing the same value twice :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)
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…

910 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

23 Experts available now in Live!

Get 1:1 Help Now