Example 1: jquery validator driving license
$.validator.addMethod("Drive_License", function (value, element) {
var Tipos_Licenca = ['A', 'A1', 'A2', 'AM', 'B', 'B1', 'BE','C','C1','C1E','D','D1','D1E','DE'];
var i = 0;
var Licencas_Inseridas = value.toUpperCase().split(",");
while (i < Licencas_Inseridas.length) {
if (!Tipos_Licenca.includes(Licencas_Inseridas[i])) {
return false;
}
i++;
}
return true;
})
Example 2: jquery validation plugin
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>
Example 3: 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 4: jquery validator Url
$.validator.addMethod("UrlVal", function (value, element) {
var Inicio_URL = ['http://www', 'https://www', 'www', 'ftp://www'];
var Fim_URL = ['com', 'net', 'org', 'pt', 'eu','co'];
var Url_Inserido = value.toLowerCase().split(".");
if (Inicio_URL.includes(Url_Inserido[0])) {
if (Fim_URL.includes(Url_Inserido[(Url_Inserido.length - 1)])) {
return true;
} else
return false;
} else
return false;
})
Example 5: 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 6: jquery form validation
function submitFunction(event){
event.preventDefault();
}
$("#form_id").submit(submitFunction);