2016-08-08 44 views
0

我正在使用客户端验证的Rails窗体。我怎么可以自定义错误信息,当前说:Rails自定义客户端验证消息

“值必须等于或大于......”

这里是表单字段:

<%= f.number_field :age, placeholder:"Age", class:"form-control", required: true, max:90, min:17, message: 'foo' %> 

回答

0

HTML5 API可以设置custom error validation message。看下面的例子:

实施例:

<form> 
    <label for="mail">I would like you to provide me an e-mail</label> 
    <input type="email" id="mail" name="mail"> 
    <button>Submit</button> 
</form> 

,然后加入一个JS:

var email = document.getElementById("mail"); 

email.addEventListener("keyup", function (event) { 
    if (email.validity.typeMismatch) { 
    email.setCustomValidity("I expect an e-mail, darling!"); 
    } else { 
    email.setCustomValidity(""); 
    } 
}); 

setCustomValidity()的文档。