Skip to content

Instantly share code, notes, and snippets.

@xdougx
Last active August 29, 2015 14:01
Show Gist options
  • Save xdougx/e97341fc3bc6451fabd0 to your computer and use it in GitHub Desktop.
Save xdougx/e97341fc3bc6451fabd0 to your computer and use it in GitHub Desktop.
Validations.js
var Validations = (function(){
var Validation = function() {
var name = $("[name='nome']");
var email = $("[name='email']");
var empresa = $("[name='empresa']");
var telefone = $("[name='telefone']");
var mensagem = $("[name='mensagem']");
var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
this.validate = function() {
try {
this.validate_presence_of(name);
this.validate_presence_of(email);
this.validate_presence_of(empresa);
this.validate_presence_of(telefone);
this.validate_presence_of(mensagem);
this.validate_format_of(email, regex);
return true;
}catch(e) {
$(".error").fadeIn("fast", function(){
$(this).html(e);
});
return false;
}
}
this.validate_presence_of = function(e) {
if(e.val() == ""){
e.focus();
throw field(e.attr("name")) + " deve ser preenchido";
}else {
return true;
}
}
this.validate_format_of = function(e, regex) {
if(!regex.test(e.val())){
throw field(e.attr("name")) + " está em um formato inválido";
}else {
return true
}
}
var field = function (e) {
return "<strong>" + e[0].toUpperCase() + e.slice(1) + "</strong>";
}
};
return Validation;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment