How to Bind Data when a Textbox Loses Focus?

I need to validate a user-entered value against a value retrieved from the database using a JavaScript function. Below is the code snippet I am currently using:

<asp:TextBox ID="txtNoDays" runat="server" Width="49px" onblur="return NumericChk(this,'<%=v_days%>')"></asp:TextBox>

Answer №1

My recommendation is to utilize ASP.NET controls such as RangeValidator or CompareValidator.

Here is an example for you:

Markup

<head runat="server">
    <title></title>
    <script type="text/javascript>
        function NumericChk(obj, val) {
            if (obj.value > val) {
                obj.focus();
                return false;
            }
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" 
                     runat="server">         
        </asp:TextBox>
    </form>
</body>

Make sure to add the "onblur" attribute using code-behind.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int value = 10;
            string handle=string.Format("return NumericChk(this,{0})",value);
            TextBox1.Attributes.Add("onblur", handle);
        }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In Javascript, have an audio clip play every 30 seconds

My webpage has a unique feature - it automatically plays a short audio clip as soon as the page loads. This functionality is achieved using a special audio player called nifty player. Here is the code snippet for embedding the player: <object classid= ...

Is there a lifecycle function in React Native that runs when the app is not actively being used or is in the background?

Is there a function in react native that runs continuously even when the app is not being used or in background mode? I am looking for a lifecycle function in react-native that operates even when the user is not actively using the app or when the app is r ...

Solving problems with asynchronous programming in Express.js

I'm struggling with the asynchronous nature of Node.js. In my code, I have a function like this: var sendData = function(req, res) { var requestId = req.params.id; var dataForSend = []; database.collection('songs').find({player_ ...

"Embrace the Tempus Dominus integration for the latest version of Bootstrap -

After reading through the script documentation, I attempted to implement it on my example page. However, I keep encountering the error: Uncaught TypeError: $(...).datetimepicker is not a function. Here's the code snippet that I included in the head s ...

Initiate the Html.BeginForm process in an asynchronous manner

Within my ASP.NET MVC 3 application, I am attempting to upload a file using the Html.BeginForm helper, as shown below: <% using (Html.BeginForm("ImportFile", "Home", new { someId = Id }, FormMethod.Post, new { enctype="multipart/form-data" } )) %> ...

Obtain the ClientID for a particular user control that is within a repeater's bindings

I have a collection of user controls that I am connecting to a repeater. The user control: (Example) "AppProduct" <div> <asp:Button ID="btn_details" runat="server" Text="Trigger" /> <asp:HiddenField ID="pid" ...

Rearrange object based on several criteria - JavaScript

var numbers = { "value": [{ "num1": 1, "num2": 10 }, { "num1": 15, "num2": 13 }, { "num1": 26, "num2": 24 }, { "num1": 6, "num2": 25 }, { "num1": 15, "num2": 20 ...

The chosenValues.filter method hit an unexpected token; a comma was anticipated

I have encountered a syntax error in the following line: queryComponents: prevState.seletedValues.filter((a, i) => (i !== index)); I am attempting to swap out splice with filter. I've attempted modifying brackets, both adding and removing them, b ...

The efficiency of WebMethod decreases

Currently, I am working on a basic JavaScript AJAX request using jQuery: $.ajax({ type: "POST", url: "TabbedSummaryPage.aspx/RunReport", data: "{'itemId': '', 'lType': '', 'reportId': '&ap ...

Node: How can I retrieve the value of a JSON key that contains a filename with a dot in

I have a JSON file named myjson.json, with the following structure: { "main.css": "main-4zgjrhtr.css", "main.js": "main-76gfhdgsj.js" "normalkey" : "somevalue" } The purpose is to link revision builds to their original filenames. Now I need to acce ...

The asynchronous nature of how setInterval operates

I am working with a setInterval function that executes asynchronous code to make calls to the server: setInterval(()=> { //run AJAX function here }, 5000); In scenarios where the server does not receive a response within 5 seconds, there is a like ...

html displaying dynamic data in a table

As a beginner in coding, I am attempting to create a dynamic table. The first column is working fine with each new cell being added to the top. However, when I try to add columns, they fill from top to bottom instead of mirroring the first column. I want m ...

Image expanded on a spherical surface

How can I resolve the issue of my image being stretched out and pixelated when using three.js to cover a sphere with a photo? Here's the code snippet causing the problem: moonSurface = new THREE.Mesh( new THREE.SphereGeometry(moonRadius -.1, 50, ...

When adding margin-left and margin-right, images do not appear in their designated positions

I have a chart displaying images, which are showing up correctly. However, I am facing an issue when I try to add some spacing to the chart by using margin-left and margin-right. Here is the CSS code I included: #chart1 { margin: 0 auto; ...

What is the proper way to combine two arrays containing objects together?

I am faced with the challenge of merging arrays and objects. Here is an example array I am working with: [ { name: "test", sub: { name: "asdf", sub: {} } }, { name: "models", sub: {} } ] ...

Utilizing an object or object key as a parameter for a component in VueJs

My Personnel table is connected to an array of objects from VueJs. The last column in the table has an edit button for each record. I want to display a modal popup when the edit button is clicked, where the textboxes are linked to the personnel properties ...

Error: JavaScript alert not displaying

My asp.net page is not displaying the Javascript alert message until I clear the browser's cache memory. Can someone please explain why this happens and suggest a solution? ...

Selenium Assistance: I'm encountering a scenario where on a webpage, two elements share the same Xpath, making it difficult to differentiate them based on even

At index [1], both elements are identified, but at index [2], nothing is identified. The key difference between the two is that one has display:none, and the other has display:block. However, their involvement in determining these fields is minimal due to ...

There seems to be this strange and unexpected sharing of Animated.View and useRef between different child components

Currently, I am displaying a list of items in the following manner: {formattedJournal[meal].map((food, idx, arr) => { const isLast = idx === arr.length - 1; return ( <View key={idx}> ...

The Facebook SDK fails to activate in Internet Explorer

I am currently working on implementing a Facebook login using the JavaScript SDK. Everything is functioning correctly in most browsers, but I am experiencing issues with certain versions of Internet Explorer. The login functionality is not working on my l ...