使用 jquery 进行动态文本框验证

发布于 2024-10-08 03:37:19 字数 682 浏览 0 评论 0原文

我的表单中有动态文本框。我喜欢使用 bassistance jquery 插件进行验证。

http://bassistance.de/jquery-plugins/jquery-plugin-validation /

我的文本框名称位于数组格式设置[]中,如何表示规则和消息

<input type="text" name="setting[]">

我的验证脚本是

<script type="text/javascript">
$("#freebidz_frm").validate({
    rules: {
        setting:{
            required: true
            }   
    },
    messages: {
        setting: '<br>'+"Please enter  Default Font Style"+''
    }});
</script>

但它不起作用......有人帮助我!提前致谢

I am having dynamic text box in my form . I like to validate using bassistance jquery plugin.

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

My textbox name is in array formate setting[], how to represent rules and messages

<input type="text" name="setting[]">

my validation script is

<script type="text/javascript">
$("#freebidz_frm").validate({
    rules: {
        setting:{
            required: true
            }   
    },
    messages: {
        setting: '<br>'+"Please enter  Default Font Style"+''
    }});
</script>

But it's not working ... somebody help me ! Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-10-15 03:37:19

不管其他人怎么说,在输入名称中包含方括号是有效的(而且有用)(但是对于 ID 无效)。大多数服务器端语言会将它们作为数组进行处理,这可以在编写处理时节省大量时间。

至于你的脚本,有两点可能会出错:

  1. 确保验证脚本包含在 jQuery 的 $(document).ready(function(){ ... }); 中。 (我假设您的完整脚本中有它)
  2. 如果有任何特殊字符,请尝试将输入名称(包括方括号)括在引号中。

我已将您的代码复制到新的 HTML 页面中,并且当我进行这两项调整时,验证脚本可以正常工作。请确保在脚本的“规则”和“消息”部分中将“设置[]”括在引号中。

由于您是根据输入创建数组,因此我假设这不是表单中唯一的“setting[]”。如果是这种情况,您将不得不变得更花哨 - 创建类规则可以帮助您验证基于公共类的一组输入(http://docs.jquery.com/Plugins/Validation/Validator/addClassRules) 。

Despite what others are saying, it's valid (and useful) to include square brackets in your input names (invalid for IDs, however). Most server-side languages will process these as an array, which can save you a lot of time when writing the processing.

As for your script, there are two points where it may be going wrong:

  1. Make sure the validation script is wrapped in jQuery's $(document).ready(function(){ ... }); (I'm assuming you have it in your full script)
  2. In the case of any special characters, try wrapping the input name (including square brackets) in quotes.

I've copied your code into a new HTML page and the validation script worked properly when I made these two adjustments. Just be sure to wrap 'setting[]' in quotes in both the 'rules' and 'messages' parts of the script.

Since you're creating arrays from your inputs, I'm assuming this isn't the only 'setting[]' in the form. If that's the case, you'll have to get a little fancier - creating class rules can help you validate a set of inputs based on a common class (http://docs.jquery.com/Plugins/Validation/Validator/addClassRules).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文