jquery.validate.form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>basic validation of form</title>
<link rel="stylesheet" href="../css/kurs.css">
<script src="http://flodhest.net/merge/main.js" type="text/javascript"></script>
<script type="text/javascript">
/*
* Inline javascript is a no-no
* $.log() is a custom jquery method
*/
$(function() {
$("form").submit(function() {
var has_data = false;
$(this).find(":input").not("input[type=submit]").each(function() {
if(this.value.length) has_data = true;
});
$(this).find("input[type=submit]").val(has_data ? "yes" : "no");
$.log("form has data? " + (has_data ? "yes" : "no"));
// alternative solution
$.log( alternative_solution(this) ? "+ has data" : "- no data" );
return false;
});
});
alternative_solution = function(obj) {
return $(obj).find(':input[type!=submit]')
.filter(':input[value!=]')
.length ? true : false;
}
</script>
</head>
<body>
<h1>Check if form has data</h1>
<form>
<input type="text">
<input type="file">
<input type="submit" value="has data?">
</form>
</body>
</html>