Solved

Assistance with JavaScript/jQuery validation

Posted on 2014-07-18
5
269 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
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)

707 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

15 Experts available now in Live!

Get 1:1 Help Now