Skip to main content

How to change control's background color when validation fails when controls have multiple validation

Here is the java script :

function ContainsControl(controlArray,arrayItem) {
    var result = { boolValue: false, indexValue: -1 };
    for (var i = 0; i < controlArray.length; i++) {
        if (controlArray[i]["control"] == arrayItem.control) {
            result.boolValue = true;
            result.indexValue = i;
            break;
        }
    }
    return result;
}
 
function SetBGColor() {
    var inputControlValidationResult = new Array();
    for (var i = 0; i < Page_Validators.length; i++) {
        var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
        var arrayItem = { control: inputControl, valid: Page_Validators[i].isvalid ? 1 : 0 };
        var result = ContainsControl(inputControlValidationResult, arrayItem);
        if (result.boolValue) {
            inputControlValidationResult[result.indexValue].valid &= arrayItem.valid;
        }
        else {
            inputControlValidationResult.push(arrayItem);
        }
      
    }
    for (var i2=0; i2 < inputControlValidationResult.length; i2++) {
        inputControlValidationResult[i2].control.style.backgroundColor = inputControlValidationResult[i2].valid ? "" : "red";
    }
}
 
In order to run this script after client side validation but before page
 submission, you have to register this using some of the server side 
page events, I use the Page_Load :
 
protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterOnSubmitStatement(GetType(), "onsubmit", "SetBGColor();");
        } 

Comments

Popular posts from this blog

Asp.Net AjaxFileUpload Control With Drag Drop And Progress Bar

This Example explains how to use AjaxFileUpload Control With Drag Drop And Progress Bar Functionality In Asp.Net 2.0 3.5 4.0 C# And VB.NET. Previous Post  I was Explained about the   jQuery - Allow Alphanumeric (Alphabets & Numbers) Characters in Textbox using JavaScript  ,  Fileupload show selected file in label when file selected  ,  Check for file size with JavaScript before uploading  . May 2012 release of AjaxControlToolkit includes a new AjaxFileUpload Control  which supports Multiple File Upload, Progress Bar and Drag And Drop functionality. These new features are supported by Google Chrome version 16+, Firefox 8+ , Safari 5+ and Internet explorer 10 + , IE9 or earlier does not support this feature. To start with it, download and put latest AjaxControlToolkit.dll in Bin folder of application, Place ToolkitScriptManager  and AjaxFileUpload on the page. HTML SOURCE < asp:ToolkitScriptManager I...

Check dot net core framework version in my PC Or System

 Open My Computer → double click "C:" drive → double click "Windows" → double click "Microsoft.NET" → double click "Framework" → Inside this folder, there will be folder(s) like "v1.0.3705" and/or "v2.0.50727" and/or "v3.5" and/or "v4.0.30319". Your latest .NET version would be in the highest v number folder, so if v4.0.30319 is available that would hold your latest .NET framework. However, the v4.0.30319 does not mean that you have the .NET framework version 4.0. The v4.0.30319 is your Visual C# compiler version, therefore, in order to find the .NET framework version do the following. Go to a command prompt and follow this path: C:\Windows\Microsoft.NET\Framework\v4.0.30319 (or whatever the highest v number folder) C:\Windows\Microsoft.NET\Framework\v4.0.30319 > csc.exe Output: Microsoft (R) Visual C# Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporati...

ASP.NET Routing

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users. The ASP.NET MVC framework and ASP.NET Dynamic Data extend routing to provide features that are used only in MVC applications and in Dynamic Data applications. For more information about MVC, see ASP.NET MVC 3 . For more information about Dynamic Data, see ASP.NET Dynamic Data Content Map . In an ASP.NET application that does not use routing, an incoming request for a URL typically maps to a physical file that handles the request, such as an .aspx file. For example, a request for http://server/application/Products.aspx?id=4 maps to a file that is named Products.aspx that contains code and markup for rendering a response to the browser. The Web page uses the query string value of id=4 to determine what type of c...