Last active
April 4, 2017 07:29
-
-
Save herdiansc/df69226756bb42506604bb5607801375 to your computer and use it in GitHub Desktop.
Ajax form validation with parsley js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Used to validate a form with parsley | |
* Add jquery and parsley plugin before this script block | |
* Add class validated-form to the form | |
* Add class 'ajax-parsley-form' to the ajax form | |
* | |
**/ | |
var parsleyForm = $('.validated-form').parsley({ | |
errorsWrapper:'<span class="help-block"></span>', | |
errorTemplate: '<small></small>' | |
}); | |
window.Parsley.on('field:error', function() { | |
this.$element.parents('.form-group').addClass('has-error'); | |
}); | |
window.Parsley.on('field:success', function() { | |
this.$element.parents('.form-group').removeClass('has-error'); | |
}); | |
window.Parsley.on('form:error', function() { | |
$('html, body').animate({ | |
scrollTop: $('body').offset().top | |
}, 300); | |
}); | |
$(function(){ | |
$('.ajax-parsley-form button[type=submit]').bind('click',function(e){ | |
e.preventDefault(); | |
if (parsleyForm.isValid()) { | |
//if (parsleyForm.validate()) { | |
var form = $('.validated-form'); | |
$.post( form.attr('action'), form.serialize(), function(response){ | |
console.log(response); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment