Solved

adding to verification function

Posted on 2014-02-07
5
199 Views
Last Modified: 2014-02-13
I have a verification function that checks my form fields if they have value
. if it doesn't it runs a string of things.

right now i have it to check 1 field.
function validateField(fieldname, labelname, longname) {
	longname = typeof longname !== 'undefined' ? longname : fieldname;
	
	if(document.getElementById(fieldname)) {
		if(document.getElementById(fieldname).value == "" || document.getElementById(fieldname).value == $('#' + fieldname).attr('placeholder') ) {
			 themessage = (themessage + longname+ '\n');
				
		}
	}
}

Open in new window

How can I use the same verification to if 2 fields are blank.
like this: if (document.form1.model.value=="" && document.form1.model1.value=="" ) {
0
Comment
Question by:rivkamak
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39843758
function validateField(fieldID, labelname, longname) {
  longname = typeof longname !== 'undefined' ? longname : fieldID;
  var $field = $("#"+fieldID);
  if ($field) {
    if ($field.val() == "" || field.val() == $field.attr('placeholder') ) {
     return longname+ '\n');
    }
  }
  return "";
}

$(function() {
  $("#form1").on("submit",function(e) {
    var message = ""
    message += validateField("FieldID1",...);
    message += validateField("FieldID2",...);
    if (message) { 
     alert("Please fill in \n"+message);
     e.preventDefault();//cancel submission
    }
  });
});

Open in new window

0
 

Author Comment

by:rivkamak
ID: 39847556
How would I pass this using the function
if (document.form1.model.value=="" && document.form1.model1.value=="" )
validate(?????);
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39847643
My code will handle both fields if you give them an ID and call them like

   message += validateField("FieldID1","","long name");
   message += validateField("FieldID2","","long name");
0
 

Author Comment

by:rivkamak
ID: 39854192
my question is how I should call the function to begin with.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39854525
It is called for you,

It is assigned to the submit of the form

Just replace your code with mine and fill in the missing stuff

"FieldID1","here","and here");
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.

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

12 Experts available now in Live!

Get 1:1 Help Now