why form being submit plz tell me

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
khalidhabib.cs
Forum Newbie
Posts: 14
Joined: Tue Jul 17, 2012 1:35 am

why form being submit plz tell me

Post 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
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: why form being submit plz tell me

Post 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().
khalidhabib.cs
Forum Newbie
Posts: 14
Joined: Tue Jul 17, 2012 1:35 am

Re: why form being submit plz tell me

Post by khalidhabib.cs »

thanks lot :)
Post Reply