Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

adding to verification function

Posted on 2014-02-07
5
Medium Priority
?
210 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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)
Suggested Courses

876 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