Ruby on rails rails客户端只在点击保存时进行验证
我不希望客户端验证在我点击save时对输入进行模糊验证 这可能吗? 我该怎么做Ruby on rails rails客户端只在点击保存时进行验证,ruby-on-rails,client-side-validation,Ruby On Rails,Client Side Validation,我不希望客户端验证在我点击save时对输入进行模糊验证 这可能吗? 我该怎么做 我只有一些简单的验证(基本上是空的)。。。我正在考虑手写。。它值得吗?您可以使用纯Javascript或借助任何JQuery/Prototype和类似框架轻松完成 下面是我用JQuery尝试的代码 <html> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <body>
我只有一些简单的验证(基本上是空的)。。。我正在考虑手写。。它值得吗?您可以使用纯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>
$(文档).ready(函数(){
$(“#表单按钮”)。在('单击',函数()上{
var form_valid=true;
$(“#我的表单>输入[type='text']”)。每个(函数(){
如果(this.value==“”){
var error_span_present=$(this).next(“span”).length!=0;
如果(!出现错误){
$(“”+$(this).attr(“占位符”)+“不能为空”).insertAfter($(this));
}
否则{
$(this.next(“span”).html($(this.attr(“占位符”)+“不能为空”);
}
形式_valid=false;
}
否则{
if($(this).next(“span”).length!=0){
$(this.next(“span”).remove();
}
}
});
如果(表格有效){
$(“#我的表格”).submit();
}
});
});
下面是演示此代码的示例
希望这能有所帮助实际上,我只剩下HTML5验证和一些CSS。谢谢你的回答,不管怎样,它应该会起作用