javascript tinyMCE jQuery 表单验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11371966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:57:23  来源:igfitidea点击:

tinyMCE jQuery form validation

javascriptjquerytinymce

提问by el_pup_le

I'm trying to use tinymce's getContent()to make a custom validation rule, how can I do this with jquery validation? I need to apply the rule to a textarea formatted with tinymce.

我正在尝试使用 tinymce'sgetContent()来制定自定义验证规则,我该如何使用 jquery 验证来做到这一点?我需要将规则应用于使用 tinymce 格式化的 textarea。

Validation: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

验证:http: //bassistance.de/jquery-plugins/jquery-plugin-validation/

$("#element").click( function(e) {

    console.log(tinyMCE.activeEditor.getContent());

    $("#someForm").validate({
        rules: {        
            title: {
                required: true
            }
        }
    });

});

I'm thinking of just using a little bit of javascript with getContent() because it looks like there's just as much effort creating a workaround to get jquery validation working with tinymce. Thoughts on possible solutions?

我正在考虑在 getContent() 中使用一点 javascript,因为看起来创建一个解决方法来使 jquery 验证与 tinymce 一起工作需要付出同样多的努力。关于可能的解决方案的想法?

回答by Thariama

回答by J.B.Vala

Hi if your are not getting client side validation on form submit time when you are with tinymce try this code suppose your have two html editor 1 is txtAboutCompanyand 2 is txtProductinfo

嗨,如果您在使用 tinymce 时没有在表单提交时获得客户端验证,请尝试使用此代码,假设您有两个 html 编辑器 1 是 txtAboutCompanyand 2 是 txtProductinfo

this is client side code

这是客户端代码

<div class="divclass">
   @Html.LabelFor(model => model.txtAboutCompany, new { @class = "required" })
   @Html.EditorFor(model => model.txtAboutCompany)
  <span class="field-validation-error" id="AC" style="margin:9px 0 0 157px;"></span>
</div>

this is jquery

这是jquery

$("#BusinessProfile").click(function () {
        var aboutC = $("#txtAboutCompany").val()
        var pinfo = $("#txtProductinfo").val();
        if (aboutC == "" && pinfo == "") {
            $("#AC").append("").val("").html("Please enter about company")
            $("#PI").append("").val("").html("Please enter product information")
            $("#bpform").valid();

            return false;
        } else if (aboutC == "") {
            $("#PI").append("").val("").html("")
            $("#AC").append("").val("").html("Please enter about company")
            $("#txtAboutCompany").focus();

            $("#bpform").valid();
            return false;
        } else if (pinfo == "") {
            $("#AC").append("").val("").html("")
            $("#PI").append("").val("").html("Please enter product information")
            $("#txtProductinfo").focus();
            $("#bpform").valid();

            return false;
        }
        else {
            $("#AC").append("").val("").html("");
            $("#PI").append("").val("").html("");
            //return true;
            $("#bpform").validate();
        }
    });

you can get your all required validation on form submit time

您可以在表单提交时获得所有必需的验证

I know this is not proper way but you can do it .

我知道这不是正确的方法,但你可以做到。

回答by ubaid ullah

function tinymceValidation() {
    var content = tinyMCE.activeEditor.getContent();
    if (content === "" || content === null) {
        $("#questionValid").html("<span>Please enter question statement</span>");
    } else {
        $("#questionValid").html("");
    }
}

tinymce.activeEditor.on('keyup', function (e) {
    debugger;
    tinymceValidation();
});

$(form).submit(function (e) {
    tinymceValidation();
});