简体   繁体   English

jQuery,引导程序,验证和所见即所得

[英]jquery, bootstrap, validation, & wysiwyg

I have an app built on asp.net MVC and using twitters bootstrap. 我有一个基于asp.net MVC并使用twitters bootstrap构建的应用程序。 In order to get bootstrap styles to work, I have added: 为了使引导程序样式起作用,我添加了:

$.validator.setDefaults({
    highlight: function (element) {
        $(element).closest(".control-group").addClass("error");
    },
    unhighlight: function (element) {
        $(element).closest(".control-group").removeClass("error");
    }
});

So far so good. 到现在为止还挺好。 Today I added a cleditor to the site and lost validation on the textarea because cleditor hides it (as do all wysiwyg editors). 今天,我在网站上添加了一个cleditor,但由于cleditor隐藏了该文本区域,因此无法对其进行验证(所有所见即所得的编辑器都一样)。 So I added the following code which fixed that. 因此,我添加了以下代码来修复该问题。

$.validator.setDefaults({ ignore: '' });

Then I added a function which does some work on the textarea prior to validation. 然后,我添加了一个函数,该函数在验证之前会对文本区域进行一些工作。 This is a required field, so I am testing to see if it only contains spaces, non breaking spaces, and breaks. 这是必填字段,因此我正在测试以查看它是否仅包含空格,非中断空格和中断。 If so, I clear it so my validation catches it properly. 如果是这样,我将其清除,以便我的验证正确地捕获它。

$('#myForm').submit(function () {
    var contents = editor[0].$area[0].value;
    var patt1 = new RegExp('^((<br>)*(\s)*(&nbsp;)*)*$');
    if (contents.match(patt1))
        editor[0].$area[0].value = '';
    return true;
});

This function is triggered before validation, form submission is stopped and the error messages are displayed, which is all good. 在验证之前触发此功能,停止表单提交并显示错误消息,这一切都很好。 The problem, is that the validator defaults that were set above don't get triggered, so the fields aren't styled appropriately. 问题在于,上面设置的验证器默认值不会被触发,因此字段的样式不正确。

Stranger still is that if I submit the form a second time, the validator defaults are run and the fields are styled appropriately. 奇怪的是,如果我第二次提交表单,则运行验证程序的默认值,并对字段进行适当的样式设置。

I can work around this by creating a custom validation attribute in MVC, but I still would like to know why this is behaving this way, and if I can fix it. 我可以通过在MVC中创建自定义验证属性来解决此问题,但是我仍然想知道为什么这样做,以及是否可以修复它。

As a side note, the regex I am using above to find combinations of spaces and breaks etc isn't working. 顺便说一句,我在上面使用正则表达式查找空格和中断等的组合不起作用。 I have put it in two regex testers and it works fine. 我把它放在两个正则表达式测试器中,并且工作正常。 In my app, it doesn't find a match. 在我的应用中,找不到匹配项。 I am a regex novice, so there is probably a much better way to write that. 我是regex的新手,因此可能有更好的书写方式。

Try adding ignore: '', in the line directly above rules:. 尝试在规则:上方的行中添加ignore:”。 An empty string did the job for me. 一个空字符串为我完成了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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