Validation in EJ2 JavaScript Rich text editor control

18 Apr 202316 minutes to read

Validation rules

The Rich Text Editor provides the functionality of character count and its validation. So, you can validate the Rich Text Editor’s value on form submission by applying validationRules and validationMessage to the Rich Text Editor.

Rules Description
required Requires value for the Rich Text Editor 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 Rich Text Editor and the form enables the validation with the formvalidator rules by clicking on the submit externally. All rules are validated by the formvalidator rules.

var defaultRTE = new ej.richtexteditor.RichTextEditor({ 
    showCharCount: true,
    maxLength: 100,
    placeholder: 'Type something',
    change : function() {
  button.disabled = false;
}
});
defaultRTE.appendTo('#defaultRTE');

 var button = new ej.buttons.Button({
   disabled : true
 });
button.appendTo('#validateSubmit')

new ej.inputs.FormValidator('#form-element');
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Rich Text Editor</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
   <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-richtexteditor/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet">
     <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <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">
                    <textarea id="defaultRTE" name="defaultRTE" required="" maxlength="100" minlength="20" data-msg-containerid="dateError">                    </textarea>
                    <div id="dateError" style="padding-top: 10px"></div>
                </div>
                <div style="text-align: center">
                    <button id="validateSubmit">Submit</button>
                    <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
                </div>
            </form>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Validation message

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

In the following sample, customize the error message along with the concern rule.

var defaultRTE = new ej.richtexteditor.RichTextEditor({ 
  showCharCount: true,
  maxLength: 100,
  placeholder: 'Type something',
  change : function() {
   button.disabled = false;
}
});
defaultRTE.appendTo('#defaultRTE');
  
 var button = new ej.buttons.Button({
   disabled : true
 });
button.appendTo('#validateSubmit')
  
var option = {
  rules: {
    // Initialize the custom message.
    defaultRTE: { 
      required: [true, 'Type the Value'],  
      minLength: [20, 'Need atleast 6 character length'], 
      maxLength:[100, 'Maximum 100 character only']
    }  
  },
  customPlacement: function (inputElement, dateError) {
      document.getElementById('dateError').appendChild(dateError);
  }
};
  
var formObject = new ej.inputs.FormValidator('#form-element', option);
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Rich Text Editor</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-richtexteditor/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet">
     <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <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">
                    <textarea id="defaultRTE" required="" name="defaultRTE" class="form-control"></textarea>
                    <div id="dateError" style="padding-top: 10px"></div>
                </div>
                <div class="form-group">
                    <div class="col-sm-3 control-label"></div>
                    <div class="col-sm-6">
                        <div id="error"></div>
                    </div>
                </div>
                <div style="text-align: center">
                    <button id="validateSubmit" class="samplebtn e-control e-btn">Submit</button>
                    <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
                </div>
            </form>
            
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

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.

ej.base.enableRipple(true);

var defaultRTE = new ej.richtexteditor.RichTextEditor({ showCharCount: true, maxLength: 100, placeholder: 'Type something',
 change : function() {
   button.disabled = false;
} });
defaultRTE.appendTo('#defaultRTE');

 var button = new ej.buttons.Button({
   disabled : true
 });
button.appendTo('#validateSubmit')

var option = {
    rules: {
        // Initialize the CustomPlacement.
        defaultRTE: { required: [true, 'RTE: value is required'], minLength: [15, 'RTE: Need atleast 6 character length'], maxLength: [100, 'RTE: Maximum 100 character only'] }
    },
    customPlacement: function (inputElement, error) {
        document.getElementById('error').appendChild(error);
    }
};

var formObject = new ej.inputs.FormValidator('#form-element', option);
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Rich Text Editor</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-richtexteditor/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet">
     <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <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">
                    <textarea id="defaultRTE" required="" name="defaultRTE" class="form-control"></textarea>
                    <div id="dateError" style="padding-top: 10px"></div>
                </div>
                <div class="form-group">
                    <div class="col-sm-3 control-label"></div>
                    <div class="col-sm-6">
                        <div id="error"></div>
                    </div>
                </div>
                <div style="text-align: center">
                    <button id="validateSubmit" class="samplebtn e-control e-btn">Submit</button>
                    <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" data-ripple="true">Reset</button>
                </div>
            </form>
            
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>