Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
14 Aug
2022

Numeric only validation in javascript

by Shubham Batra

627

So for restricting the user from typing alphabets  in your input box we will use onkeypress event of the input box and then use js for restricting invalid characters.

Below is a code in HTML and JavaScript to number validate a text field.

<form>
    <div class="form-group row">
        <label for="inputPassword" class="col-sm-2 col-form-label">Enter Amount</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="inputNumber" placeholder="Enter Amount" value="" name="inputNumber" onkeypress="return isNumber(event)" />
        </div>
    </div>
</form>

Here is how to validate the input only to accept numbers this will only take numeric values like "112233" . Here, we are using event keycodes to know which character are invalid and which are valid ones.

<script>
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }
</script>

You can use this Complete code of HTML form and numeric function for applying the validation in js.

<form>
    <div class="form-group row">
        <label for="inputPassword" class="col-sm-2 col-form-label">Enter Amount</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="inputNumber" placeholder="Enter Amount" value="" name="inputNumber" onkeypress="return isNumber(event)" />
        </div>
    </div>
</form>
<script>
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }
</script>

So, now you can run the application and you will see you are able to enter only numeric characters now. So this is how we can implement numeric only validation in javascript.

  • |
  • Numbers validation javascript , Allow only numbers to be typed in a textbox , Number validation in JavaScript , Numric only validation in javascript

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2023 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier