Page 1 of 1

why form being submit plz tell me

Posted: Wed Jul 25, 2012 12:36 am
by khalidhabib.cs
plz tell me why form is being submit

$(document).ready(function() {
$("#frm").submit(function() {
$(":input").each(function() {
if($(this).val() === ""){
alert("Empty Fields!!");
return false;
}
});
});
});
or plz tell me hw to validate auto generated form elements in javascript

Re: why form being submit plz tell me

Posted: Wed Jul 25, 2012 2:05 pm
by tr0gd0rr
It looks like you are returning false in the wrong function. Try using Event#preventDefault like so:

Code: Select all

$(document).ready(function() {
	$("#frm").submit(function(event/* must get event object here*/) {
		$(":input").each(function() {
			if ($(this).val() === "") {
				// prevent the default action--which for forms is submission
				event.preventDefault();
				alert("Empty Fields!!");
			}
		});
	});
});
Keep in mind that you will get one alert for every empty field. If you just want one alert, you need to throw an exception or iterate $(':input') with a traditional for loop instead of jQuery#each().

Re: why form being submit plz tell me

Posted: Tue Jul 31, 2012 4:56 am
by khalidhabib.cs
thanks lot :)