Solved

adding to verification function

Posted on 2014-02-07
5
201 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

27 Experts available now in Live!

Get 1:1 Help Now