我使用带有客户端验证的rails表单。如何自定义错误消息,当前显示:
"值必须等于或大于......"
以下是表单字段:
<%= f.number_field :age, placeholder:"Age", class:"form-control", required: true, max:90, min:17, message: 'foo' %>
答案 0 :(得分: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("");
}
});