Solved

Assistance with JavaScript/jQuery validation

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Video Tutorial help 2 38
How to get text of href without any ID using Javascript 9 24
Two decimal 5 26
setInterval() calls function twice in one interval 2 36
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

749 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