validation jquery code example
Example 1: jquery validation example
reference : https://www.sitepoint.com/basic-jquery-form-validation-tutorial/
$(function() {
$("form[name='registration']").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
}
},
messages: {
firstname: "Please enter your firstname",
lastname: "Please enter your lastname",
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
email: "Please enter a valid email address"
},
submitHandler: function(form) {
form.submit();
}
});
});
Example 2: form validation using jquery
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
then add javascript code:
$(document).ready(function() {
$("#form").validate();
});
</script>
Example 3: invoking jquery validator
$(document).ready(function () {
$('#myform').validate({
});
$('#button').click(function() {
if ($('#myform').valid()) {
alert('form is valid - not submitted');
} else {
alert('form is not valid');
}
});
});
Example 4: jquery form validation
function submitFunction(event){
event.preventDefault();
}
$("#form_id").submit(submitFunction);
Example 5: form validation using jquery
jQuery(document).ready(function() {
jQuery("#forms).validate({
rules: {
firstname: 'required',
lastname: 'required',
u_email: {
required: true,
email: true,
maxlength: 255,
},
}
});
});
Example 6: jquery validate
$("#myform").validate({
debug: true
});