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

How to apply two different validation in same column in ASP.NET MVC Grid

Hi,

I have two columns A and B, I want to do a validation for the column B. If the column A the value selected is "L" I should be able to enter alphanumeric values in the column B, otherwise it should only allow decimal values to be entered. How can to do that?

I am using EJ2 controls.

Thanks,

1 Reply

RS Rajapandiyan Settu Syncfusion Team May 8, 2020 12:28 PM UTC

Hi Daniel, 
 
Greetings from syncfusion support. 
 
Query :  I have two columns A and B, I want to do a validation for the column B. If the column A the value selected is "L" I should be able to enter alphanumeric values in the column B, otherwise it should only allow decimal values to be entered. How can to do that? 
 
As per your requirement we have bind two different validation for the same column EmployeeID depends on the ShipCountry column’s value in the actionBegin event of beginedit. Please refer the below code example and sample for more information.   
 
 
@Html.EJS().Grid("Validation").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col => 
{ 
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").ValidationRules(new { required = "true"}).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
     
    col.Field("ShipCountry").HeaderText("Ship Country").EditType("dropdownedit").Width("150").Add(); 
    col.Field("EmployeeID").Type("s").HeaderText("EmployeeID").Width("150").Add(); 
    col.Field("Freight").HeaderText("Freight").Width("150").Add(); 
}). ActionBegin("ActionBegin").AllowPaging().PageSettings(page => page.PageCount(2)).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render() 
 
 
<script> 
    function ActionBegin(args) { 
        if(args.requestType == 'beginEdit'){ 
            var gridobj = document.getElementsByClassName('e-grid')[0].ej2_instances[0]; 
            if (args.rowData["ShipCountry"] == "Germany") { 
                gridobj.getColumnByField("EmployeeID").validationRules = { required: true, minLength: [customFn1, 'Enter alphanumeric values'] }; 
            } 
            else { 
                gridobj.getColumnByField("EmployeeID").validationRules = { required: true, minLength: [customFn2, 'Enter decimal value'] }; 
            } 
        } 
    } 
 
    function customFn1(args) { 
        val = parseFloat(args['value']); 
        return (args['value'].match(/^[A-Z0-9]+$/)); // bind the condition as you want 
    } 
    function customFn2(args) { 
        val = parseFloat(args['value']);         
        return (args['value'].match(/^[0-9_.]+$/)) && (val - Math.floor(val)) != 0; // bind the condition as you want 
    }; 
</script> 
 
 
Screenshot #1 : shipcountry value – Denmark & employeeID column have decimal value validation  
 
 
 
Screenshot #2 : shipcountry value – Germany & employeeID column have alphanumeric value validation 
 
 
 
 
Please get back to us if you need further assistance on this. 
 
Regards, 
Rajapandiyan S 


Loader.
Up arrow icon