Solved

Assistance with JavaScript/jQuery validation

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

803 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