Solved

adding to verification function

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

810 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