验证可编辑字段

发布于 2024-08-25 03:34:53 字数 570 浏览 3 评论 0原文

我使用 Jeditable (发布到 CakePHP)在我的页面上进行输入。我希望用户只在 Jeditable 字段中填写数字,所以我的想法是还使用 jQuery 验证插件< /a> 来验证是否仅使用数字,我已经在网站的其他部分使用了它。

当您单击 div 时,Jeditable 会动态创建一个带有输入的表单,因此 Jquery 验证似乎没有任何内容可以绑定,并且它似乎无法正常工作。

我可以通过 Jeditable 设置表单的类名(它只有一个类属性),创建的输入只有一个名称属性,默认为“名称”。

我的问题:

  • 这是只需要数字的好方法吗?
  • 如果是这样,如何使其与 Jquery validate 一起使用
  • 如果不是,什么是更好的解决方案?

提前致谢,我期待得到任何答案;)

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 ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

豆芽 2024-09-01 03:34:53

您可以直接在 jeditable 中使用 jQuery 验证插件来验证表单的字段

$('.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());
    }
});

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());
    }
});
烟若柳尘 2024-09-01 03:34:53

这段代码对我有用

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

$('.edit').editable(your_url, {
    onsubmit: function(settings, td) {
      var input = $(td).find('input');
        var original = input.val();
        if (isNumeric(original)) {
            console.log("Validation correct");
            return true;
        } else {
            console.log("Validation failed. Ignoring");
            input.css('background-color','#c00').css('color','#fff');
            return false;
        }
    }
});

this code worked for me

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

$('.edit').editable(your_url, {
    onsubmit: function(settings, td) {
      var input = $(td).find('input');
        var original = input.val();
        if (isNumeric(original)) {
            console.log("Validation correct");
            return true;
        } else {
            console.log("Validation failed. Ignoring");
            input.css('background-color','#c00').css('color','#fff');
            return false;
        }
    }
});
め七分饶幸 2024-09-01 03:34:53

您可以使用 Jeditable 中的 onsubmit 事件并检查输入是否只有数字。
这只是一个例子,我没有尝试过:

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

$('.edit').editable(your_url, {
    onsubmit: function(settings, original) {
        if (isNumeric(original)) {
            return true;
        } else {
            //display your message
            return false;
        }
    }
});

You can use the onsubmit event in Jeditable and check if the input has only numbers.
This is just an example, I didn't try it:

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

$('.edit').editable(your_url, {
    onsubmit: function(settings, original) {
        if (isNumeric(original)) {
            return true;
        } else {
            //display your message
            return false;
        }
    }
});
坦然微笑 2024-09-01 03:34:53

对于我的项目,我已将函数名称更改为 isAcceptable,因为我允许括号、破折号、点作为电话号码的一部分。

function isAcceptable(value) {
  if (value == null || !value.toString().match(/^[-+]?\d*\.?[\d\s().-]+$/)) return false;
  return true;
}

For my project I've changed the function name to isAcceptable because I allow brackets, dashes, dots to be part of the phone number.

function isAcceptable(value) {
  if (value == null || !value.toString().match(/^[-+]?\d*\.?[\d\s().-]+$/)) return false;
  return true;
}
一刻暧昧 2024-09-01 03:34:53

打开 jquery.editable.js 并找到:onedit 函数

/* setup some functions */
        var onedit   = settings.onedit   || function() { }; 

并执行以下操作:

$('.edit').editable(your_url, {
    onedit:function(){
            alert("original value"+$(this).text());
   },
});

Open jquery.editable.js and find: onedit function

/* setup some functions */
        var onedit   = settings.onedit   || function() { }; 

and do some like:

$('.edit').editable(your_url, {
    onedit:function(){
            alert("original value"+$(this).text());
   },
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文