Tag: asp.net validation

Change back ground color of Asp.net Textbox on validation failure

Here is a way of changing back ground color for Textbox on validation failure. The key here is using Page_ClientValidate function in javascript.
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ErrorMessage=”Please enter a value”
ControlToValidate=”TextBox1″></asp:RequiredFieldValidator>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” OnClientClick=”btnClick();”
OnClick=”Button1_Click1″ />
<script type=”text/javascript”>
function btnClick()
{
if (!Page_ClientValidate())
{
document.getElementById(“TextBox1″).style.backgroundColor=”red”;
}
}

function ClearBackGround()
{
document.getElementById(“TextBox1″).style.backgroundColor=”";
}
</script>

In Code behind

TextBox1.Attributes.Add(“onkeypress”, “ClearBackGround()”);


Types of ASP.NET Validations

By default, ASP.NET Web pages automatically validate that malicious users are not attempting to send script to your application. For more information, see Script Exploits Overview.

Type of validation Control to use Description
Required entry RequiredFieldValidator Ensures that the user does not skip an entry. For details, see How to: Validate Required Entries for ASP.NET Server Controls.
Comparison to a value CompareValidator Compares a user’s entry against a constant value, against the value of another control (using a comparison operator such as less than, equal, or greater than), or for a specific data type. For details, see How to: Validate Against a Specific Value for ASP.NET Server Controls and How to: Validate Against a Data Type for ASP.NET Server Controls.

  • Cloud World

  • Custom Search



  • Cyberbrutus
    iDream theme by Templates Next | Powered by WordPress

    Switch to our mobile site