我有这段代码:
<script>
$(document).ready(function(){
$('form').validate({
//rules: { 'input_name': { email: true} },
rules: { 'name^=input', 'name$=jander': { email: true} },
messages: { 'input_name': "Jar" },
errorPlacement: function(error, element) {
error.insertBefore(element);
},
debug:true
})
});
</script>
</head>
<body>
<div id="jar">fasdf</div>
<form id="clar">
<p id="my_paragraph">Escribe ALGO para que haga la validacion de email</p>
<input name='input_jander' type="text" ">
<input type="submit" >
</form>
正如您所看到的,我尝试将验证规则应用于名称属性以“input”开头的所有输入字段和以“jander”结尾,但它不起作用(错误消息未显示)..
答案 0 :(得分:1)
只需提供您想要定位一个类的输入并以这种方式定位它就会容易得多。为什么要让自己变得更难
答案 1 :(得分:0)
这些键只是映射到表单的name
属性。
然而,我之前遇到过这个问题,我就这样解决了......
var rules = {},
messages = {},
inputs = $(':input[name^="input"]').filter(':input[name$="jander"]');
inputs.each(function() {
var name = $(this).attr('name');
rules[name] = { email: true };
messages[name] = { email: 'Type a proper email, mate!' };
});
$('form').validate({
'rules': rules,
'messages': messages
});
jsFiddle input
个匹配的元素。
虽然你应该为这些元素提出一个更健全的标识符。