开发者

Jquery validation directly inside Form Element setup?

开发者 https://www.devze.com 2023-03-25 10:51 出处:网络
Is it possible to have a jquery validation code on a form element directly inside the form ele开发者_如何学运维ment?

Is it possible to have a jquery validation code on a form element directly inside the form ele开发者_如何学运维ment?

Example, say I have a textbox... Could I set it up like this...

<input type="submit" name="submit" validation="Here is where you place Jquery Validation Code that would execute onSubit of form">

I thought I saw that online some where while I was searching for something else... It would be great if you could do that, because it would really be what I need... Unfortunately, life is never so sweet...

But say you were generating your form elements on the fly, and you wanted each individual element to have a slightly different validation code to be run by jquery that would give you a false or a true if not met... How would you manage to do that?

Thanks, Derek


I dont know about doing it like that but have you looked at this plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/ ?

Basicly, if you would have HTML like this:

<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" />
</p>

And the form this is in has id signupForm, you would just do this:

$("#signupForm").validate({
    rules: {
        firstname: {
            required: true,
            minlength: 2
        }
    },
    messages: {
        firstname: {
            required: "Please enter your firstname",
            minlength: "Your firstname must consist of at least 2 characters"
        }
    }
});

Edited example with 2 rules. See the plugin documentation for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消