Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

javascript - How to validate input fields with a dot in name using the jquery validation plugin?

I'm using this jquery validation plugin

<s:textfield cssClass="form-control input-default" name="contest.title" id="title" placeholder="Enter Title"
                             />

Validation doesn't work for this, but if I change the name to title - validation works.

I tried searching, but couldn't find a means to validate fields with a . in their name.

Please help

Update

Script

<script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery("#contestform").validate({
                    submitHandler: function(form) {
//                        return false;  // block the default submit action
                    },
                    rules: {
                        title: {
                            required: true
                        },
                        link: {
                            required: true
                        },
                        startdt: {
                            required: true
                        },
                        enddt: {
                            required: true
                        },
                        descr: {
                            required: true
                        },
                    },
                    messages: {
                        title: "Please enter a title",
                        link: "Please enter the sponser redirection link",
                        startdt: "Please select start date",
                        enddt: "Please select end date",
                        descr: "Please enter description"
                    }
                });
            });
        </script>

Part of the form

<form action="" enctype="multipart/form-data" method="post" id="contestform">
            <s:hidden name="option" value="option"/>
            <s:hidden name="contest.idcontest"/>
            <div class="form-group">
                <label for="title">Title</label>
                <s:textfield cssClass="form-control input-default" name="contest.title" id="title" placeholder="Enter Title"
                             />
            </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to put the field names in qoutes. From the plugin documentation

Fields with complex names (brackets, dots)

When you have a name attribute like user[name], make sure to put the name in quotes. More details in the General Guidelines.

The sample in the linked reference:

$("#myform").validate({
  rules: {
    // no quoting necessary
    name: "required",
    // quoting necessary!
    "user[email]": "email",
    // dots need quoting, too!
    "user.address.street": "required"
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...