This is a migrated thread and some comments may be shown as answers.

Problem with Validator

2 Answers 57 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gabriel
Top achievements
Rank 1
Gabriel asked on 10 Dec 2011, 11:48 PM
Since there is no dedicated forum to the Validator, I'll post it here.

I have a form with a Validator applied to a text input. Works well except for submitting the form. According to the docos "Note that if a HTML form element is set as validation container, the form submits will be automatically prevented if validation fails."

This is not the case as my form submits anyway. I'm checking for false on the button click at the moment, but it doesn't work for anything. I would like submisson of forms to be disabled as per the documentation.

Any ideas?

@<div id="searchform">
    <form method="get" action="@Href("~/Collection/Search")" class="search">
    <ul>
        <li>
            <input type="text" name="q" value="@q" class="k-textbox" required />
        </li>
        <li>
            <button class="k-button" id="search">Search</button>
        </li>
    </ul>
    </form>
    <div class="status">
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            var validator = $("#searchform").kendoValidator({
                rules: {
                    custom: function (input) {
                        return input.val().length > 3;
                    }
                },
                messages: {
                    custom: "You need 3 or more characters",
                    required: "Oops, you forgot me!",
                    email: function (input) {
                        return getMessage(input);
                    }
                }
            }).data("kendoValidator");
            $("#search").click(function () {
                if (validator.validate() == false) {
 
                }
            });
        });
    </script>
</div>

2 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 12 Dec 2011, 01:03 PM
Hello Gabriel,

As stated in the documentation in order automatic prevent of form submit to work, the validator should instantiated on the form element, instead as in the code you have pasted where an div element is used. 


All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gabriel
Top achievements
Rank 1
answered on 13 Dec 2011, 12:48 AM
Thanks, fixed and all works very well!
Tags
General Discussions
Asked by
Gabriel
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Gabriel
Top achievements
Rank 1
Share this question
or