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
why form being submit plz tell me
Moderator: General Moderators
-
khalidhabib.cs
- Forum Newbie
- Posts: 14
- Joined: Tue Jul 17, 2012 1:35 am
Re: why form being submit plz tell me
It looks like you are returning false in the wrong function. Try using Event#preventDefault like so:
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().
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!!");
}
});
});
});-
khalidhabib.cs
- Forum Newbie
- Posts: 14
- Joined: Tue Jul 17, 2012 1:35 am
Re: why form being submit plz tell me
thanks lot 