Created
July 6, 2013 20:36
-
-
Save tcelestino/5941192 to your computer and use it in GitHub Desktop.
Validate forms data with jQuery without plugin
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
<!doctype html> | |
<html lang="pt_BR"> | |
<head> | |
<meta charset="UTF-8"> | |
<link rel="stylesheet" href="style.css"> | |
<title>Validate form with jQuery</title> | |
</head> | |
<body> | |
<form action="" method="post" id="formExample"> | |
<fieldset> | |
<ol> | |
<li> | |
<label for="name" class="label01">Name</label> | |
<input type="text" name="name" id="name" /> | |
<span class="erro" style="display:none">Your name</span> </li> | |
<li> | |
<label for="email" class="label01">Email</label> | |
<input type="email" name="email" id="email" /> | |
<span class="erro" style="display:none">Your email</span> | |
</li> | |
<li><button type="submit" name="send_data" class="alignright btn01">Send</button> </li> | |
</ol> | |
</fieldset> | |
</form> | |
<script src="http://code.jquery.com/jquery.js"></script> | |
<script src="validate.js"></script> | |
<script> | |
(function(){ | |
$form = $('#formExample'); | |
$form.on('submit', function(evt){ | |
var that = $(this); | |
if(validate(that)) { | |
console.log('ok!'); | |
} else { | |
console.log('fields empty'); | |
} | |
evt.preventDefault(); | |
}) | |
}); | |
</script> | |
</body> | |
</html> |
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
body { | |
margin: 0; | |
padding: 0; | |
} | |
.error-field { | |
border:1px solid #C00 | |
} | |
.error { | |
display:block; | |
margin:10px 0 10px 150px; | |
color:#C00; | |
} |
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
function validate(selector, show_errors) { | |
var show_errors = (show_errors!=undefined)?show_errors:true; | |
var valid = true; | |
$(selector).find("input, textarea, select").each(function() { | |
var $field = $(this); | |
var $exe = $('.exe'); | |
var $error = $field.parents("li:first").find(".erro"); | |
var _val = ($field[0].tagName == "SELECT")?$field.find("option:selected").val():$field.val(); | |
if ($.trim(_val) == '' && $error.size()>0) { | |
if(show_errors) { | |
$field.addClass("error-field"); | |
$error.fadeIn(); | |
$exe.hide(); | |
$field.focus(function() { | |
$field.removeClass("error-field"); | |
$error.fadeOut(); | |
}); | |
} | |
valid = false; | |
} | |
}); | |
return valid; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment