Allow for the use of dots and tabs within textboxes

I have implemented text boxes that only allow numeric values with the help of JavaScript. However, I am facing an issue where I cannot enter a dot symbol or use tab movement. How can I modify this code to include these functionalities?


function CheckNumeric(e) {
    if (window.event) { // For IE 
        if ((e.keyCode < 48 || e.keyCode > 57) && e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 9) {
            event.returnValue = false;
            return false;
        }
    } else { // For FireFox
        if ((e.which < 48 || e.which > 57) && e.which != 8 && e.which != 46 && e.which != 9) {
            e.preventDefault();
            return false;
        }
    }
}

Answer №1

Tested and proven code that can assist you!

  <script type="text/javascript">
      function onlyDotsAndNumbers(txt, event) {
          var charCode = (event.which) ? event.which : event.keyCode
          if (charCode == 46) {
              if (txt.value.indexOf(".") < 0)
                  return true;
              else
                  return false;
          }
          if (charCode > 31 && (charCode < 48 || charCode > 57))
              return false;

          return true;
      }

> <asp:TextBoxID="txt1"runat="server"onkeypress="return onlyDotsAndNumbers(this,event)"/>

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

navigate back to the previous tab using protractor

When I open a new tab (second), I attempt to switch back to the first tab. common.clickOpenNewSession(); //opens a new tab browser.getAllWindowHandles().then(function (handles) { var secondWindowHandle = handles[1]; var firstWindowHandle ...

Examining the potential of a promise within a dynamic import feature in Angular

Here's a code snippet that I'm working with: The component file (component.ts) looks like this: async ngOnInit() { import('dom-to-image').then(module => { const domToImage = module.default; const node = document.getEl ...

Styling a <slot> within a child component in Vue.js 3.x: Tips and tricks

I'm currently working on customizing the appearance of a p tag that is placed inside a child component using the slot. Parent Component Code: <template> <BasicButton content="Test 1234" @click="SendMessage('test') ...

How to efficiently pass dynamic props in Next.js persisting layoutuciary

When setting up a persistence layout, I utilize the getLayout function on each page as shown below: import { Layout } from 'hoc'; const Page = () => { return ( <div>hello</div> ); }; Page.getLayout = function getLayout(pa ...

Update child1's props using a callback function in React and then pass the updated value to child2

Within my application, I have a child component called 'Menu' which updates a 'select' state through a click event. This function is implemented as follows: Menus.jsx (child component): import React, { Component } from 'react&apo ...

Tips on gathering information from an HTML for:

After encountering countless programming obstacles, I believe that the solution to my current issue is likely a simple fix related to syntax. However, despite numerous attempts, I have been unable to resolve it thus far. I recently created a contact form ...

Using JQuery to initiate specific events based on the clicked element

I have a row of images displayed and I'm looking to show a different list item when each picture is clicked. Here's what I have done so far, check out the demo. https://jsfiddle.net/UniqueCoder/3975prLy/1/ $("img:nth-child(1)").on('click&a ...

Experiencing issues integrating Strapi with Next.JS, encountering an unexpected error

I have encountered an issue with setting up the configuration between Strapi and Next.JS. Despite configuring everything correctly in Strapi, I am unable to receive the API in Next.JS and display it on the screen. Instead, I keep getting an error message w ...

What could be causing the failure of the update for this computed property in my Vue 3 application?

Currently, I am in the process of creating an audio player using Vue 3 and the Napster API. About the Project I have managed to make the vinyl rotate by utilizing a CSS keyframes-based animation paired with the computed property isSpinning. I intend for ...

What causes the disparity in height between a textbox and the encompassing span element?

What causes the discrepancy in element heights when looking at the code below? <span id="spanner" style="margin:0;padding:0;border:0;outline:0;"><input type="text" /></span> If you check the height of the text box, it will be shown as 1 ...

Obtaining values from non-ASP controls within an ASP.NET project

Could someone please provide guidance on obtaining values from non-ASP controls in my C# code? Any help would be greatly appreciated. Thank you! ...

What is the best way to completely eliminate a div from a webpage

I've created a new div element using jQuery: $(document.body).append('<div id="test"></div>'); Then, I display a success message and remove the div after 10 seconds: $('test').html('SUCCESS!'); setT ...

Leveraging Facebook data within yii framework

I am currently working on extracting data from a user who has registered using Facebook with Yii framework. I have managed to fetch the data, but now I want to understand how to extract specific data from the facebook.js class and store it in a hidden fiel ...

Some inquiries regarding the fundamentals of JavaScript - the significance of the dollar sign ($) and the error message "is not a function"

Teaching myself JavaScript without actually doing any explicit research on it (crazy, right?) has led to a few mysteries I can't quite crack. One of these mysteries involves the elusive dollar sign. From what I gather, it's supposed to be a con ...

Sorting DataGridView columns based on matching strings

I am trying to implement a feature in my datagridview where the rows are sorted based on a user-entered search string. The search string is compared with the strings in a specific column, and the rows are sorted in descending order based on the matching cr ...

Modify the size of images while shuffling using Javascript

Hey there! I've got some bootstrap thumbnails set up and I'm using a script to shuffle the images inside the thumbnails or a element within the li. It's working fine, but some of the images are coming out larger or smaller than others. I&apo ...

Unlocking the Power of JavaScript: Harnessing Its Potential for Numerous IDs in HTML

There's a script I came across on the internet at this link: let multi = 2; let str = "Little lamb"; let multiStr = ""; while(multi > 0){ multiStr += str multiStr += " "; multi--; } multiStr = multiStr.trimEnd ...

Resolving the Enigma: Querying jQuery for Real-Time Validation and

I'm fairly new to jQuery and I'm facing a challenge in my registration form script. Specifically, I want to check if the entered username or email is already taken while the user is typing. Currently, this functionality works by making a json req ...

Having trouble using the elementIsNotVisible method in Selenium WebDriver with JavaScript

I'm struggling to detect the absence of an element using the elementIsNotVisible condition in the Selenium JavaScript Webdriver. This condition requires a webdriver.WebElement object, which is problematic because the element may have already disappear ...

The initial click does not trigger a state update in React

I attempted to create a straightforward system for displaying data with two sorting buttons (ascending & descending). My approach involved fetching and displaying data from an array using the map method. In a separate component file, I utilized useEffect ...