Adding javascript to the OnBlur property of an ASP.NET text box control

javascript

To add JavaScript to the OnBlur property of an ASP.NET text box control, you can follow these steps:

  1. In your ASP.NET markup code, add the OnBlur property to the text box control:
<asp:TextBox runat="server" ID="myTextBox" OnBlur="myFunction()"></asp:TextBox>

In this example, myFunction is the name of the JavaScript function that will be called when the text box loses focus.

  1. In a script block at the bottom of your page, define the myFunction function:
<script>
    function myFunction() {
        // Your JavaScript code here
    }
</script>

In this function, you can write any JavaScript code that you want to execute when the text box loses focus.

For example, you could use this function to validate the input in the text box and display an error message if necessary:

<script>
    function myFunction() {
        var textBox = document.getElementById("myTextBox");
        var inputValue = textBox.value;
        if (inputValue.length < 5) {
            alert("Input must be at least 5 characters long");
            textBox.focus();
        }
    }
</script>

In this example, if the length of the input in the text box is less than 5 characters, an alert message will be displayed and the focus will be returned to the text box so that the user can correct the input.