jQuery Validate() 特殊函数
我正在对某些表单使用 jQuery validate()
插件,效果非常好。唯一的问题是我有一个输入字段需要特殊的验证过程。流程如下:
在 domready 中为所有必填字段调用 jQuery 验证插件。
这是一个输入示例:
<li>
<label for="nome">Nome completo*</label>
<input name="nome" type="text" id="nome" class="required"/>
</li>
以下是我如何调用特殊函数:
<li>
<span id="sprytextfield1">
<label for="cpf">CPF* (xxxxxxxxxxx)</label>
<input name="cpf" type="text" id="cpf" maxlength="15" class="required" />
<span class="textfieldInvalidFormatMsg">CPF Inválido.</span>
</span>
</li>
在文件底部我调用 Spry 函数:
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1","cpf");
//-->
</script>
当然,我在 head 部分以及我的 < 中调用 Spry CSS 和 JavaScript 文件代码>special-validate.js。
当我只使用 jQuery validate()
插件并单击发送按钮时,页面会自动返回到第一个错误的输入字段并显示错误类型(不是数字、不是有效的电子邮件等) .)。
但是对于这个新函数,“返回第一个错误”功能当然不起作用,因为 validate()
函数认为这一切都很好。
我已经为另一种形式添加了一条规则(关于图片上传),它是这样的:
$("#commentForm").validate({
rules: {
foto34: {
required: true,
accept: "jpg|png|gif"
}
}
});
现在我的问题是,如何添加特殊验证功能作为整个验证过程的规则?
这是更好地理解它的页面:链接文本,特殊字段是第一个:公积金。
我希望我清楚地解释了我的问题。
I'm using the jQuery validate()
plugin for some forms and it goes great. The only thing is that I have an input field that requires a special validation process. Here is how it goes:
The jQuery validate plugin is called in the domready for all the required fields.
Here is an example for an input:
<li>
<label for="nome">Nome completo*</label>
<input name="nome" type="text" id="nome" class="required"/>
</li>
And here is how I call my special function:
<li>
<span id="sprytextfield1">
<label for="cpf">CPF* (xxxxxxxxxxx)</label>
<input name="cpf" type="text" id="cpf" maxlength="15" class="required" />
<span class="textfieldInvalidFormatMsg">CPF Inválido.</span>
</span>
</li>
And at the bottom of the file I call the Spry function:
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1","cpf");
//-->
</script>
Of course I call the Spry CSS and JavaScript files in the head section as well as my special-validate.js
.
When I just use the jQuery validate()
plugin and click on the send button, the page goes automatically back to the first mistaken input field and shows the error type (not a number, not a valid email etc.).
But with this new function, this "going-back-to-the-first-mistake" feature doesn't work, of course, because the validate()
function sees it all good.
I already added a rule for another form (about pictures upload) and it goes like this:
$("#commentForm").validate({
rules: {
foto34: {
required: true,
accept: "jpg|png|gif"
}
}
});
Now my question is, how can I add the special validation function as a rule of the whole validation process?
Here is the page to understand it better: link text and the special field is the first one: CPF.
I hope I was clear explaining my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将回答我自己的问题,因为我刚刚找到了解决方案。
我在 DOM 准备好打开之前添加了 JavaScript 函数来检查我的 cpf 号码(巴西身份证号码),
如下所示: 然后我的 DOM 准备好:
如果某些巴西人确实感兴趣,我可以展示执行 CPF 验证的整个函数(用我自己的号码检查,有效)。我还有另一个 CNPJ 号码(巴西公司号码)。
I'll answer my own question as I just found the solution.
I added the JavaScript function to check my cpf number (Brazilian ID number) just before the DOM ready opening like this:
and then here goes my DOM ready:
If it does interest some Brazilians, I can show the whole function that does the CPF validation (checked with my own number, works). I also have another one for the CNPJ number (Brazilian number for companies).