Created
December 1, 2016 12:11
-
-
Save solancer/740cd5412ebdb19de4848b5265b1d936 to your computer and use it in GitHub Desktop.
jquery validate rules
This file contains 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
// Validation wrappe init & rules | |
$("#form").validate({ | |
errorClass: 'invalid', | |
validClass: 'valid', | |
errorPlacement: function(error, element) { | |
error.insertAfter($(element)); | |
// error.insertAfter($(element).siblings('label')); | |
}, | |
rules: { | |
'name': { | |
required: true, | |
minlength: 2, | |
maxlength: 25 | |
}, | |
'mobile': { | |
required: true, | |
minlength: 10, | |
maxlength: 10 | |
},'email': { | |
required: true, | |
email: true, | |
},'q-message': { | |
required: true, | |
maxlength: 1000, | |
minlength: 20 | |
}, | |
submitHandler: function (form) { | |
$("#myModal").modal("show"); | |
setTimeout(function() { | |
createRecaptcha(); | |
}, 100); | |
function verifyCallback() { | |
$("#myModal").modal("hide"); | |
var request; | |
// bind to the submit event of our form | |
// let's select and cache all the fields | |
var $inputs = $(form).find("input, select, button, textarea"); | |
// serialize the data in the form | |
var serializedData = $(form).serialize(); | |
// fire off the request to /form.php | |
request = $.ajax({ | |
url: "sendmail/send.php", | |
type: "post", | |
data: serializedData | |
}); | |
// callback handler that will be called on success | |
request.done(function (response, textStatus, jqXHR) { | |
document.getElementById("quote-form").reset(); | |
// log a message to the console | |
toastr["success"]("Form Submitted Sucessfully!"); | |
$(form).find("input:text").removeClass('used'); | |
// alert("Form Submitted Sucessfully!"); | |
}); | |
// callback handler that will be called on failure | |
request.fail(function (jqXHR, textStatus, errorThrown) { | |
// log the error to the console | |
console.error( | |
"The following error occured: " + textStatus, errorThrown); | |
}); | |
// callback handler that will be called regardless | |
// if the request failed or succeeded | |
request.always(function () { | |
// reenable the inputs | |
$inputs.prop("disabled", false); | |
}); | |
} | |
function createRecaptcha() { | |
grecaptcha.render("captcha", {sitekey: "your-site-recaptcha-key", theme: "light",'callback' : verifyCallback,}); | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment