Solved

Assistance with JavaScript/jQuery validation

Posted on 2014-07-18
5
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

690 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