Skip to content

Instantly share code, notes, and snippets.

@maaddae
Created August 24, 2018 09:02
Show Gist options
  • Save maaddae/4238c1346496aed26ae9315338fdf460 to your computer and use it in GitHub Desktop.
Save maaddae/4238c1346496aed26ae9315338fdf460 to your computer and use it in GitHub Desktop.
An asynchronous validation of registration form using jquery-validate plugin.
var searchVisible = 0;
var transparent = true;
var mobile_device = false;
$(document).ready(function(){
$.material.init();
/* Activate the tooltips */
//$('[rel="tooltip"]').tooltip();
// Code for the Validator
var $validator = $('.wizard-card form').validate({
rules: {
firstname: {
required: true,
minlength: 3,
letters: true
},
lastname: {
required: true,
minlength: 3,
letters: true
},
email: {
required: true,
minlength: 3,
},
username: {
required: true,
minlength: 3,
alphachars: true,
remote: {
url: "src/retreive-username.php",
type: "post"
}
},
company_name: {
required: true,
minlength: 3
},
contact: {
required: true,
minlength: 10,
maxlength: 12,
digits: true
},
password: {
required: true,
haspchars: true,
minlength: 8,
},
confirm_password: {
required: true,
equalTo: "#password"
}
},
messages: {
firstname: {
letters: "Only letters and spaces are allowed",
minlength: "Your name is too short"
},
lastname: {
letters: "Only letters and spaces are allowed",
minlength: "Your name is too short"
},
username: {
alphachars: "Only alphanumeric and underscore are allowed",
minlength: "Your name is too short",
remote: "Username already in use!"
},
company_name: {
minlength: "Your name is too short"
},
contact: {
digits: "Only digits are allowed"
},
password: {
minlength: "Your password is too short",
haspchars: "Password must contain = ! - @ . _ * "
},
confirm_password: {
equalTo: "Passwords do not match"
}
},
errorPlacement: function(error, element) {
$(element).parent('div').addClass('has-error');
error.insertAfter(element);
}
});
$.validator.addMethod("letters", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z\s]*$/);
});
$.validator.addMethod("alphachars", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z0-9_]*$/);
});
$.validator.addMethod("haspchars", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
//&& /[a-z]/.test(value) // has a lowercase letter
//&& /\d/.test(value) // has a digit
&& /[=!\-@._*]/.test(value) // has a special character
});
$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
&& /[a-z]/.test(value) // has a lowercase letter
&& /\d/.test(value) // has a digit
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment