Enabling the use of dot or full stop in JavaScript keycode for numeric input

I am facing a challenge with a method that is supposed to allow all numeric values, including the '.' symbol. Below is the code for my method. While it successfully allows numeric values, it seems to have an issue with keyCode == 190.

function IsNumeric(e) {
    var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
    var ret = (keyCode >= 48 && keyCode <= 57 && keyCode == 190);
    document.getElementById("error_numeric").style.display = ret ? "none" : "inline";
    return ret;
}

<input class="form-control" name="teacher_cnic" value="" onkeypress="return IsNumeric(event);" type="text" placeholder="12345.1234567.1" required>

This method is called within a form using

onkeypress="return IsAlphaNumeric(event);"

Answer №1

Replace && with || in the second condition for keyCode

function IsNumeric(e) {
        var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
        var ret = ((keyCode >= 48 || keyCode <= 57) || keyCode == 190);
        document.getElementById("error_numeric").style.display = ret ? "none" : "inline";
        return ret;
    }

Answer №2

When the onkeypress event is triggered, the decimal or full stop point keyCode is keyCode=46.

Therefore, the function's conditional logic will be as follows:

var result = ((keyCode >= 48 && keyCode <= 57) || keyCode == 46);

This effectively solves the problem at hand.

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

Kendo popup event binding issue arises after upgrading React to version 17.0.1

After upgrading my application from react version 16.4.1 to 17.0.1, I encountered an issue where a function is not being called when clicking a button. Strangely, the function works fine if the component is not transformed into a Kendo window using jQuery. ...

What are the steps to transforming a regular expression into a dynamic format?

I've developed a powerful regex that locates specific text within a lengthy string without spaces. Now I'm attempting to utilize it dynamically to search for various words, but I can't seem to make it function as intended. Check out my rege ...

Leveraging the power of React Native with embedded RapidAPI functionality in the source

I had previously used the following code to retrieve a JSON file containing personal data in my React Native source code: async componentDidMount() { try { const response = await fetch('mydomain.org/personaldata.json'); const responseJson ...

A comprehensive guide on using ajax to reproduce a Postman POST API request

Currently, I am able to retrieve an "access_token" using Postman; however, I am attempting to recreate this process in ajax for experimentation purposes on jsfiddle. In Postman, the following setup is used: A POST request URL: No active headers Body in ...

Retrieving an Ajax response by using form.submit() and sending it to PHP

After spending hours trying to mix a form.submit() call with a jquery/ajax call in order to receive a response from my php login script, I am at a loss. Despite looking through countless posts and examples on the same topic, I can't seem to find a sol ...

The 404 error is showing up when attempting to access 'socket.io/socket.io.js' through Express.js

I'm currently working on implementing a live user count feature on my website. You can check it out at . The backend is built using Express JS, but I encountered an error while trying to install socket.io: GET /socket.io/socket.io.js 404 1.911 ms - 1 ...

Creating a JSON object from HTML tag attributes using jQuery

My table contains rows with various attributes that I need to access using jQuery when the row is clicked. For example, here is a sample <tr> element: <tr data-id="1" data-employeeid="4" data-acceess="none" data-area="HR"> <td class="so ...

Retrieve any webpage using AJAX

Hey there! I'm a beginner in AJAX and have a question that may seem basic to some. I understand that you can set up a page to respond to an AJAX call, but is it possible to request any page with AJAX? In other words, can all the functionalities of a ...

Angular and Bootstrap combined to create a versatile navigation bar with a collapsible sidebar feature

Currently, I am in the process of developing a progressive web app using Angular and Bootstrap. One of the main challenges I am facing is implementing a navbar that not only looks great on desktop but also on mobile devices. So far, I am satisfied with my ...

Once the user clicks on the download button, the backend should initiate the download process

I am seeking a solution where a download can be triggered in the background when a download button is clicked, without interrupting other tasks. Is there a way to achieve this in PHP? If so, what method can be used? I have attempted using the curl function ...

Struggling with running a jQuery ajax request inside a function?

Below is my code for a jQuery Change Event: $("input[name=evnt_typ]").change(function(){ var request = $.ajax({ method: "POST", url: "ajaxRequest.php", dataType: "json ...

Utilizing jQuery for JSON parsing

Within my JavaScript code, I am working with the following array: var versions = [{"id":"454","name":"jack"}, {"id":"4","name":"rose"} {"id":"6","name":"ikma"} {"id":"5","name":"naki"} {"id":"667","name":"dasi"} ] I need to extract the name from this ar ...

Differences between Global and Local Variables in Middleware Development

While exploring ways to manage globally used data in my research, I stumbled upon this question: See 2. Answer After integrating the suggested approach into my codebase, I encountered a problem that I would like to discuss and seek help for. I created a ...

Unable to reach a variable within the class itself

I'm facing an issue with my MobX store. In my Store class, when I try to access this.user.permits.db, I get an error stating that this.user is undefined. I am confused as to why I can't access the @observable user. src/ui/store/store.js file: ...

When running the command "npx create-next-app@latest --ts," be aware that there are 3 high severity vulnerabilities present. One of the vulnerabilities is that "node-fetch

Just set up a fresh project with Next.js and TypeScript by following the documentation using npx create-next-app@latest --ts. Encountering multiple high severity vulnerabilities despite running npm audit fix --force, which actually adds more vulnerabiliti ...

Executing a nested function as an Onclick event in an HTML document

I am currently working on a project within the Phone Gap framework where I need to transfer values from one HTML page to another. My solution involved using the code snippet below. var searchString = window.location.search.substring(1),i, val, params = s ...

adaptable web design flexible picture

I am facing an issue while using MVC razor code to create a website logo and adjusting the image scaling for resizing the website. Even after trying image = max-width:100% and height:auto, the resizing is not working as expected! Below is the code snippet ...

Having trouble with my Angular subscription - not behaving as I anticipated

I am facing an issue on my shop page where I have a FilterBarComponent as a child component. On initialization, I want it to emit all the categories so that all products are rendered by default. However, on my HomePageComponent, there is a button that allo ...

How to Utilize Muuri Javascript Library with Bootstrap Tabs: Resizing Required for Expansion?

Struggling for days and feeling frustrated, I'm hoping someone can help me crack this mystery. My idea is straightforward - three Bootstrap 4 tabs with drag and drop functionality inside each, all displayed in a responsive stacked masonry layout. To a ...

Checking for an empty value with javascript: A step-by-step guide

Below is an HTML code snippet for checking for empty or null values in a text field: function myFormValidation() { alert("Hello"); var name = document.getElementById("name").value; alert(name); if (name == null || name == "") { document.ge ...