We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

specify maximum length of text box depending on the value selected from dropdownlist

Hello Syncfusion Support Team,

I have this Syncfusion dropdownlist and a text box which has value of nameLength (using data-bind knockout value binding)

what i need is when the user select "name1" value from dropdownlist , the max length of nameLength should be equal to 5.
otherwise when the user select "name2" value from dropdownlist , the max length of nameLength should be equal to 10.
when the user select "name3" value from dropdownlist , the max length of nameLength should be equal to 15.

so that , if the user select "name1" , and try to enter a value in text box >5 or <5 number of characters(max length ) , a red error message "cannot insert more than or less than 5 characters" will appear under this textbox .

thanks in advanced.


self.nameLength = ko.observable();


        var items = [
            { name: "name1", id: "1" },
            { name: "name2", id: "2" },
            { name: "name3", id: "3" }
        ];
        $('#nationality').ejDropDownList({
            dataSource: items,
            fields: { text: "name", value: "id" },
            select: function (args) {
                console.log("Value is " + args.value);
            },
            watermarkText: "names",
            width: "100%"
        });



     name:
     


1 Reply

AB Ashokkumar Balasubramanian Syncfusion Team July 22, 2019 01:25 PM UTC

Hi Ali Ossaily, 
 
We analyzed your requirement of set validation rule based on the selection in dropdownlist. We suggested you update the value of each dropdownlist and pass that value to the validationRules in maskedtextbox. 
 
$('#dropdown1').ejDropDownList({ 
 
            dataSource: items, 
 
            fields: { text: "name", value: "id" }, 
 
            validationRules: { required: true }, 
 
            change: function (args) { 
 
                var length; 
 
                if (args.value === "1") length = 5; 
 
                else if (args.value === "2") length = 10; 
 
                else if (args.value === "3") length = 15; 
 
                maskInstance.option('validationRules.value', length); 
 
            } 
 
        }); 
 
Check the condition in validationRules, whether entered character is equal to the value in dropdownlist, if it doesn't show the error message. 
  
$.validator.addMethod("value", 
            function (value, element, params) { 
                if (!/Invalid|NaN/.test(value)) { 
                    return parseInt(value.length) == params; 
                } 
            }, jQuery.validator.format("cannot insert more than or less than {0} characters")); 
 
We have prepared sample for your reference, refer this link 
 
 
Please check the above provided sample and get back to us, if you require any further assistance on this. 
 
Regards, 
Ashokkumar B. 


Loader.
Live Chat Icon For mobile
Up arrow icon