Solved

adding to verification function

Posted on 2014-02-07
5
206 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
[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
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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)
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).

617 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