My question is i want to validate my form in a javascript object oriented way and i am unable to make it work.
THIS IS THE FIDDLE
HTML:------------------------------------------------------------------------------------------------------
<form id="contact-form" action="#" method="get">
<ul>
<li><input type="text" id='fname' name='fname' class='required' placeholder="Firstname"/></li>
<li><input type="text" id='lname' name='lname' class='required' placeholder="Lastname"/></li>
<li><input type="password" id='pwd' name='pwd' class='required' placeholder="Password"/></li>
<li><input type="password" id='confPwd' name='confPwd' class='required' placeholder="Confirm password"/></li>
<li><input type="text" id='email' name='email' class='required' placeholder="Email"/></li>
<li><input type="text" id='website' name='website' class='required' placeholder="Website"/></li>
<li>
<input type="submit" id='submit' value="Submit" />
</li>
</ul>
</form>
JS & JQUERY:---------------------------------------------------------------------------------------------
var formValidate = {
validate: function(elem) {
var frm = elem.attr('id');
$('.required', '#' + frm).each(function() {
if ($(this).val() === '') {
alert($(this).attr('id') + ' is a required field.'); //<---gets here
$(this).focus();
return false; // <--not returning false
}
});
}
};
$(function() {
$('form').submit(function() {
formValidate.validate($('form'));
});
});