Reputation: 8366
I have this ajax form with name, email and message and I want to validate the inputs:
<script type="text/javascript">
$(document).ready(function(){
$form = $('form');
$form.submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
$("#inlocuire").fadeOut(1000, function () {
$(this).html("<img src='images/de_multumire.png'/>").fadeIn(2000);
});
var message = $('textarea[name=message]').val('');
var name = $('input[name=name]').val('');
var email = $('input[name=email]').val('');
},'json');
return false;
});
});
I tried to introduce this function but it is not working:
$('#form_id').ajaxForm({
beforeSubmit: validate
});
function validate(formData, jqForm, options) {
var name = $('input[name=name]').fieldValue();
var email = $('input[name=email]').fieldValue();
var message = $('textarea[name=message]').fieldValue();
if (!name[0]) {
alert('Please enter a value for name');
return false;
}
if (!email[0]) {
alert('Please enter a value for email');
return false;
}
if (!message[0]) {
alert('Please enter a value for message');
return false;
}
else {
// here to send the form
}
The form is working fine...
Upvotes: 1
Views: 1588
Reputation: 8366
for those having the same problem as me: http://docs.jquery.com/Plugins/Validation
Upvotes: 1