简体   繁体   中英

jQuery validation without “form” tag

According to http://docs.jquery.com/Plugins/Validation the "form" tag is necessary in order to do validation. In my case I don't have form tag. How can I validate(required field) my textbox on click of "button" type control

Why not just add a form tag? If it's an input, then it should normally be part of a form.

You could always wrap it with a fake form and validate it.

var $textbox = $("#textbox");
$("<form>").append($textbox).validate();

Note, however, that in most cases this should imply that we're going about something wrongly, and I'd consider a form for every element that's submitted in any form (whether it's through standard GET/POST, AJAX, etc.).

I know, it is quite old question, anyway, I had almost the same problem: I had defined form:

<form id="some-form-id"></form>

And than in document I had inputs like this:

<input type="text" form="some-form-id" />

jQuery validator cannot validate this, because elements weren't within the form, so I made small update: There is method elements which load elements to validate and I edit it into version bellow this text. I add loading items which are not inside form, which I load into variable outForm . This items are loaded only if form has attribute id . I test it and it works. I hope that this will help to someone.

elements: function () {
                var validator = this,
                    rulesCache = {};

                // select all valid inputs inside the form (no submit or reset buttons)
                var inForm = $(this.currentForm)
                    .find("input, select, textarea")
                    .not(":submit, :reset, :image, [disabled], [readonly]")
                    .not(this.settings.ignore)
                    .filter(function () {
                        if (!this.name && validator.settings.debug && window.console) {
                            console.error("%o has no name assigned", this);
                        }

                        // select only the first element for each name, and only those with rules specified
                        if (this.name in rulesCache || !validator.objectLength($(this).rules())) {
                            return false;
                        }

                        rulesCache[this.name] = true;
                        return true;
                    });

                var formId = $(this.currentForm).attr('id');
                if(typeof formId == 'undefined')
                    return inForm;

                var outForm = $("input[form='"+formId+"'], select[form='"+formId+"'], textarea[form='"+formId+"']")
                    .not(":submit, :reset, :image, [disabled], [readonly]")
                    .not(this.settings.ignore)
                    .filter(function () {
                        if (!this.name && validator.settings.debug && window.console) {
                            console.error("%o has no name assigned", this);
                        }

                        // select only the first element for each name, and only those with rules specified
                        if (this.name in rulesCache || !validator.objectLength($(this).rules())) {
                            return false;
                        }

                        rulesCache[this.name] = true;
                        return true;
                    });

                return $.merge(inForm,outForm);

            },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM