开发者

What's the easiest way to do form validation for jQuery?

开发者_开发技巧All I want to do is to check if the textfield has been changed. If not, I want to highlight the boxes when submit is pressed. How do I do that? Seems very simple, but not sure why every other solution is so complicated.


Building upon Pim's answer, you can associate the changed flag for each text field using jQuery's data API.

// initially, assign changed to false for all text fields
$("input:text").data("changed", false);

// if any field changes, set its changed flag to true
$("input:text").change(function() {
    $(this).data("changed", true);
}

// finally on submission, get all text fields that did not
//  change by checking their "changed" property
var unchangedItems = $("input:text").filter(function() {
    return $(this).data("changed") === false;
});


You can use the jQuery Validate plugin.


Bassicly, you just say it has never been changed, and when it cahnges, you set a flag saying is has been changed:

var changed = false;
$('#textfield').change(function(){
    changed = true;
});
if(changed){
    $('.textbox').each(function(){
        $(this).addClass('.highlighted');
        //or something like this, whatever you want to do to highlight them
    });
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜