0
我不希望客户端验证在模糊输入时进行验证,只是当我点击保存时。rails client_side_validations only hit hit save
这可能吗? 我该怎么做?
我只是一些简单的验证(空的,基本上)...而我正在考虑用手写它..它值得吗?
我不希望客户端验证在模糊输入时进行验证,只是当我点击保存时。rails client_side_validations only hit hit save
这可能吗? 我该怎么做?
我只是一些简单的验证(空的,基本上)...而我正在考虑用手写它..它值得吗?
您可以使用纯Javascript或借助任何JQuery/Prototype和类似框架轻松完成此操作。
这里是我用jQuery
<html>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<body>
<form id="my_form" action="http://google.com?q=ruby" method="GET">
<input type="text" name="first_name" placeholder="First Name" /> <br/>
<input type="text" name="last_name" placeholder="Last Name" /> <br/>
<input type="text" name="city" placeholder="City" /> <br/>
<input id="form_button" type="button" value="Submit"/>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function() {
$("#form_button").on('click', function() {
var form_valid = true;
$("#my_form > input[type='text']").each(function() {
if(this.value == "") {
var error_span_present = $(this).next("span").length != 0;
if(!error_span_present) {
$("<span style='color:red;'>" + $(this).attr("placeholder") + " cannot be Blank </span>").insertAfter($(this));
}
else {
$(this).next("span").html($(this).attr("placeholder") + " cannot be Blank");
}
form_valid = false;
}
else {
if($(this).next("span").length != 0) {
$(this).next("span").remove();
}
}
});
if(form_valid){
$("#my_form").submit();
}
});
});
</script>
尝试了代码以下是演示此代码JSFiddle。
希望这会有帮助
其实,我只是留下它与HTML5验证和一些CSS。无论如何感谢您的回答,它应该=)欢呼 – caarlos0 2013-02-19 20:33:03