Validate Jeditable field

JanWillem picture JanWillem · Mar 11, 2010 · Viewed 13.8k times · Source

I'm using Jeditable (posting to CakePHP) for input on my page. I want users to only fill in numbers in the Jeditable fields, so my idea was to also use the jQuery validation plugin to validate if only numbers are used, I already use this in other parts of my site.

Jeditable dynamically creates a form with an input when you click on the div, so there seems to be nothing for Jquery validate to bind to and it doesn't seem to work as normal.

I'm able to set the class name of the form through Jeditable (it only has a class atrribute), the created input only has a name attribute which defaults to "name".

My questions:

  • Is this a good way to require only numbers?
  • If so, how to make it work with Jquery validate
  • If not, what would be a better solution?

Thanks in advance, and I look forward to any answers I might get ;)

Answer

Kamran Ali picture Kamran Ali · Dec 14, 2011

You can use jQuery validation plugin directly in jeditable to validate the fields of your form

$('.edit').editable(your_url, {
    onsubmit: function(settings, td) {
        var input = $(td).find('input');
        $(this).validate({
            rules: {
                'nameofinput': {
                    number: true
                }
            },
            messages: {
                'actionItemEntity.name': {
                    number: 'Only numbers are allowed'

                }

            }
        });

        return ($(this).valid());
    }
});