简体   繁体   English

如何在 asp.net 内核中使用 jquery 验证 ckeditor?

[英]how to validate ckeditor using jquery in asp.net core?

I want to validate a required ckeditor and when it's empty and the user submits the form it should show a message to user to fill the description.I searched a lot but none of the solutions worked.This is the jquery:我想验证所需的 ckeditor,当它为空并且用户提交表单时,它应该向用户显示一条消息以填写描述。我搜索了很多,但没有一个解决方案有效。这是 jquery:

$(document).ready(function () {

        $("#formTicket").validate(
            {
                ignore: [],
                debug: false,
                rules: {
                    Description: {
                        required: function () {
                            CKEDITOR.instances.Description.updateElement();
                        }
                    }
                },
                messages:
                {
                    Description: {
                        required: "Please enter Text"
                    }
                }
            });
    });

This is the html:这是 html:

  <form id="formTicket" asp-action="NewTicket" asp-controller="Ticket" method="post" 
          enctype="multipart/form-data">
                       
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>توضیحات*</label>
                                    <textarea rows="5" class="form-control" asp-for="Description"> 
                        </textarea>
                                    <span asp-validation-for="Description" class="text-danger"> 
                            </span>

                                </div>
                            </div>
                        </div>
                       
                        <input type="submit" id="submitButton" class="btn btn-info btn-fill pull- 
                         right" onclick="return confirm('آیا از ارسال مطمئن هستید؟')" 
                   name="submitButton" value="ارسال">
                        <div class="clearfix"></div>
                    </form>

This is the model I use for the view:这是我用于视图的 model:

public class TicketViewModel
{            
    [Required(ErrorMessage = AttributeMessages.Required)]
    public string Reciever { get; set; }             
    public string SubReciever { get; set; }      
    [Required(ErrorMessage = AttributeMessages.Required)]
    [MaxLength(200, ErrorMessage = AttributeMessages.MaxLength)]
    public string Title { get; set; }      
    [Required(ErrorMessage = AttributeMessages.Required)]
    public string Description { get; set; }
  
}

Have you added these scripts ?您是否添加了这些脚本

<script src="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>

And from your code, have you truly applied ckeditor?从你的代码来看,你真的应用了 ckeditor 吗? you should add ' class=ckeditor ' to your textarea.您应该将“ class=ckeditor ”添加到您的文本区域。

And asp-for should be replaced with name and id .并且asp-for应该替换为name 和 id

this is my demo:这是我的演示:

<form id="formTicket" asp-action="NewTicket" asp-controller="Ticket" method="post"
  enctype="multipart/form-data">
<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            <label>توضیحات*</label>
            <textarea rows="5" class="ckeditor" name="Description" id="Description" > </textarea>
            <span asp-validation-for="Description" class="text-danger">
            </span>
        </div>
    </div>
</div>
<input type="submit" id="submitButton" class="btn btn-info btn-fill pull-
                     right" onclick="return confirm('آیا از ارسال مطمئن هستید؟')"
       name="submitButton" value="ارسال">
<div class="clearfix"></div>
 </form>


@section Scripts
{
<script src="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("#formTicket").validate(
            {
                ignore: [],
                debug: false,
                rules: {
                    Description: {
                        required: function () {
                            CKEDITOR.instances.Description.updateElement();
                        },
                        minlength: 10
                    }
                },
                messages:
                {
                    Description: {
                        required: "Please enter Text",
                        minlength: 10
                    }
                }
            });
    });
</script>
}

Result:结果:

在此处输入图像描述

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

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