Search results

Validation

Validate the RichTextEditor’s value on form submission by applying Validation Rules and Validation Message to the RichTextEditor.

Validation rules

The RichTextEditor is a textarea control. The RichTextEditor also provides the functionality of character count and its validation. So, you can validate the RichTextEditor’s value on form submission by applying Validation Rules and Validation Message to the RichTextEditor.

Rules Description
required Requires value for the RichTextEditor control.
minlength Requires the value to be of given minimum characters count.
maxlength Requires the value to be of given maximum characters count.

This sample is used to validate form using the obtrusive Validation. Type the values in RichTextEditor and the form enables the validation with the formvalidator rules by clicking on the submit externally. All rules are validated by the formvalidator rules.

razor
default-validation.cs
<div class="control-section">
    <div id="content" class="box-form" style="margin: 0 auto; width:750px; padding:25px">
        <form id="form-element" class="form-vertical">
            <div class="form-group">
                @Html.EJS().RichTextEditor("defaultRTE").ShowCharCount(true).MaxLength(100).Placeholder("Type something").Created("created").Render()
                <div id="dateError" style="padding-top: 10px"></div>
            </div>
            <div style="text-align: center">
                <button id="validateSubmit" class="samplebtn e-control e-btn" type="submit" data-ripple="true">Submit</button>
                <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
            </div>
        </form>
    </div>
</div>
public IActionResult DefaultValidation()
{
    return View();
}

Validation message

The default error message for a rule can be customizable by defining it along with concern rule object as follows

razor
validation-rules.cs
<div class="control-section">
<div id="content" class="box-form" style="margin: 0 auto; width:750px; padding:25px">
    <form id="form-element" class="form-vertical">
        <div class="form-group">
            @Html.EJS().RichTextEditor("defaultRTE").ShowCharCount(true).MaxLength(100).Placeholder("Type something").Created("created").Render()
            <div id="dateError" style="padding-top: 10px"></div>
        </div>
        <div style="text-align: center">
            <button id="validateSubmit" class="samplebtn e-control e-btn" type="submit" data-ripple="true">Submit</button>
            <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
        </div>
    </form>
</div>
</div>
<script>
    var defaultRTE, formObject, options;
    function created() {
        options = {
            rules: {
                'name': {
                    required: true,
                    minLength: [6, 'Need atleast 6 character length'],
                    maxLength: [100, 'Maximum 100 character only']} } };
        formObject = new ej.inputs.FormValidator('#form-element', options);
        defaultRTE = this;
        this.element.firstChild.setAttribute("required", "");
        this.element.firstChild.setAttribute('data-required-message', '* This field is required');
        this.element.firstChild.setAttribute('data-msg-containerid', 'dateError');
        this.element.firstChild.setAttribute('name', 'name');
        
    };
    window.onload = function () {
        document.getElementById('validateSubmit').onclick = function () {
            getValue();
        };
    }
    function getValue() {
        formObject.validate();
    }
</script>
public IActionResult DefaultValidation()
{
    return View();
}

Custom placement of validation message

The FormValidator has an event customPlacement which can be used to place the error message from default position to desired custom location.

razor
custom-placement.cs
<div class="control-section">
<div id="content" class="box-form" style="margin: 0 auto; width:750px; padding:25px">
    <form id="form-element" class="form-vertical">
        <div class="form-group">
            @Html.EJS().RichTextEditor("defaultRTE").ShowCharCount(true).MaxLength(100).Placeholder("Type something").Created("created").Render()
            <div id="dateError" style="padding-top: 10px"></div>
        </div>
        <div style="text-align: center">
            <button id="validateSubmit" class="samplebtn e-control e-btn" type="submit" data-ripple="true">Submit</button>
            <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
        </div>
    </form>
</div>
</div>
<script>
    var defaultRTE, formObject, options;
    function created() {
        options = {
            rules: {
                'name': {
                    required: true,
                    minLength: [6, 'Need atleast 6 character length'],
                    maxLength: [100, 'Maximum 100 character only']} },
                    customPlacement: (this.element.firstChild, error) {
                document.getElementById('dateError').appendChild(error);
                }; };
        formObject = new ej.inputs.FormValidator('#form-element', options);
        defaultRTE = this;
        this.element.firstChild.setAttribute("required", "");
        this.element.firstChild.setAttribute('data-required-message', '* This field is required');
        this.element.firstChild.setAttribute('data-msg-containerid', 'dateError');
        this.element.firstChild.setAttribute('name', 'name');
        
    };
    window.onload = function () {
        document.getElementById('validateSubmit').onclick = function () {
            getValue();
        };
    }
    function getValue() {
        formObject.validate();
    }
</script>
public IActionResult Validation()
{
    return View();
}