Created
January 7, 2016 17:06
-
-
Save fabiorochafg/82f1db8a07bcf6e51528 to your computer and use it in GitHub Desktop.
Validation form code
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
$("form").bind("submit",function() { | |
var canSubmit = true; | |
$(":input",this).each(function() { | |
var element = $(this); | |
var type = this.type; | |
var tag = this.tagName.toLowerCase(); | |
var frase = "Por favor, preencha o campo: "; | |
if (type != "checkbox" || type != "radio") { | |
var label = $(this).prev("label").text(); | |
var campo = label.replace(':','.'); | |
} | |
if (type != "hidden" && element.css("display") != "none" && (!element.hasClass('oculto')) && (!element.hasClass('nao-obrigatorio'))) { | |
if ((type == "text" || type == "password" || type == "file" || tag == "textarea" || tag == "select") && this.value=="") { | |
window.alert(frase + campo); | |
this.focus(); | |
canSubmit = false; | |
} else if (type == "checkbox" && $("input[type='checkbox']:checked").length == 0) { | |
window.alert('Você precisa selecionar, no mínimo, uma das opções.'); | |
canSubmit = false; | |
} else if (type == "radio" && $("input[type='radio']:checked").length == 0) { | |
window.alert('Você precisa selecionar, no mínimo, uma das opções.'); | |
canSubmit = false; | |
} else if ($(this).attr("name") == "email" && ((this.value.indexOf("@") < 1))) { | |
window.alert("Por favor, preencha o campo de E-mail corretamente."); | |
this.focus(); | |
canSubmit = false; | |
} | |
return canSubmit; | |
} | |
}); | |
return canSubmit; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment