Validation Rules StringLengthRule

A validation rule that demands the target value length be within the specified value range (including the range's end points).

Type: Object

To specify the range that the validated value length must match, set the rule's min and max configuration options.

NOTE: This rule validates string values or the values that can be cast to a string.

Show Example:
AngularJS
Knockout
jQuery

In this example, the 'stringLength' rule is applied to the 'Password' editor. This rule demands that the editor value length be from 4 to 6 symbols. To check this rule, specify a password with a length out of this range.

<div style="height:200px;max-width:200px;margin:0px auto" ng-controller="demoController">
    <div dx-text-box="{ bindingOptions: {value: 'login'},  placeholder: 'Login' }" 
        dx-validator="{ validationRules: [ 
            { type: 'required', message: 'Login is required' }
        ] }">        
    </div>
    <div dx-text-box="{ bindingOptions: {value: 'password'}, mode: 'password', placeholder: 'Password' }" 
        dx-validator="{ validationRules: [ 
            { type: 'required', message: 'Password can not be empty.' },
            { type: 'stringLength', min: 4, max: 6, message: 'Password length should be in a range from 4 to 6 symbols.' }
        ] }">        
    </div>
    <div dx-validation-summary="{ }"></div>
    <div dx-button="{ text: 'Register', onClick: validateAndLogin }"></div>
</div>
var myApp = angular.module('myApp', ['dx']);
myApp.controller("demoController", function ($scope) {
    $scope.login = "Alex";
    $scope.password = "";
    $scope.validateAndLogin = function (params) {
        var result = params.validationGroup.validate();
        if (result.isValid) {
            DevExpress.ui.notify({
                message: "You are logged in as '" + $scope.login + "'",
                position: {
                    my: "left top",
                    at: "left top",
                }
            }, "Success", 3000);
        }
    }
});
angular.element(document).ready(function () {
    angular.bootstrap(document, ['myApp']);
});

In this example, the 'stringLength' rule is applied to the 'Password' editor. This rule demands that the editor value length be from 4 to 6 symbols. To check this rule, specify a password with a length out of this range.

<div style="height:200px;max-width:200px;margin:0px auto">
    <div data-bind="dxTextBox: { value: login, placeholder: 'Login' },
        dxValidator: { validationRules: [
            { type: 'required', message: 'Login is required' }
        ] }">	   
    </div>		
    <div data-bind="dxTextBox: { value: password, mode: 'password', placeholder: 'Password' },
        dxValidator: {  validationRules: [
            { type: 'required', message: 'Password can not be empty.' },
            { type: 'stringLength', min: 4, max: 6, message: 'Password length should be in a range from 4 to 6 symbols.' }
        ] }">
    </div>
    <div data-bind="dxValidationSummary: { }"></div>
    <div data-bind="dxButton: { text: 'Register', onClick: validateAndLogin }"></div>
</div>
var myViewModel = {
    login: ko.observable("Alex"),
    password: ko.observable(""),
    validateAndLogin: function (params) {
        var result = params.validationGroup.validate();
        if (result.isValid) {
            DevExpress.ui.notify({
                message: "You are logged in as '" + this.login() + "'",
                position: {
                    my: "left top",
                    at: "left top",
                }
            }, "Success", 3000);
        }
    },
}
ko.applyBindings(myViewModel);

In this example, the 'stringLength' rule is applied to the 'Password' editor. This rule demands that the editor value length be from 4 to 6 symbols. To check this rule, specify a password with a length out of this range.

<div style="height:200px;max-width:200px;margin:0px auto">
    <div id="login"></div>
    <div id="password"></div>
    <div id="summary"></div>
    <div id="button"></div>
</div>
$(function () {
    $("#login").dxTextBox({
        value: 'Alex', placeholder: 'Login'
    }).dxValidator({
        validationRules: [{ type: 'required', message: 'Login is required' }]
    });
    $("#password").dxTextBox({
        value: null, mode: 'password', placeholder: 'Password'
    }).dxValidator({
        validationRules: [
            { type: 'required', message: 'Password can not be empty.' },
            { type: 'stringLength', min: 4, max: 6, message: 'Password length should be in a range from 4 to 6 symbols.' }
        ]
    });
    $("#summary").dxValidationSummary({});
    $("#button").dxButton({
        text: 'Submit',
        onClick: function (params) {
            var result = params.validationGroup.validate();
            if (result.isValid) {
                var login = $("#login").dxTextBox('instance').option('value');
                DevExpress.ui.notify({
                    message: "You are logged in as '" + login + "'",
                    position: {
                        my: "left top",
                        at: "left top",
                    }
                }, "Success", 3000);
            }
        }
    });
});

max

Specifies the maximum length allowed for the validated value.

Type: Number

message

Specifies the message that is shown for end-users if the current rule is broken.

Type: String
Default Value: 'The length of the value is not correct'

You do not have to specify this field. If you choose not to specify it, a default message will be shown. To include the name of the validated editor into the default validation message as a subject, set the name field of the dxValidator object.

min

Specifies the minimum length allowed for the validated value.

Type: Number

trim

Indicates whether or not to remove the Space characters from the validated value.

Type: Boolean
Default Value: true

To keep the Space characters within the validated value, set this field to false.

type

Specifies the type of the current rule.

Type: String
Accepted Values: 'stringLength'

Set this field to 'stringLength' to define a rule that will be broken if the validated value length does not match to the range specified by the min and max configuration options.