How to Allow Only 4-digit Numbers in Textbox using JQuery?

Introduction

Have you ever wanted to restrict the input in a textbox to only allow 4-digit numbers? In this tutorial, we will explore how to achieve this using JQuery, a popular JavaScript library. By following the steps below, you will be able to enhance the user experience on your website by ensuring that users can only enter valid 4-digit numbers.

Step:- Create a HTML Page

<!DOCTYPE html>
<html>
<head>
    <title>Allow only 4 numbers in textbox using Jquery - wizbrand</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body> 
    
<form>
  <h1>Allow only 4 numbers in textbox using Jquery - wizbrand</h1>
  <input type="text" name="phone" placeholder="4-digit numbers" maxlength="4" class="phone">
     
  <button type="submit">Submit</button>
</form>
    
</body>
    
<script type="text/javascript">
  $('.phone').keypress(function(e) {
      var arr = [];
      var kk = e.which;
   
      for (i = 48; i < 58; i++)
          arr.push(i);
   
      if (!(arr.indexOf(kk)>=0))
          e.preventDefault();
  });
</script>
</html>

Output:-

Hopefully, It will help you to set a password..!!!

Related Posts

How to Buy and Fly Drones in India: Simple Guide for Buyers and Pilots

Flying machines create real excitement across India today. Many people see new career paths open up with these tools. Young students dream of pilot jobs. Farmers want…

Read More

Travel eSIM Comparison Guide: Find Cheap International Data Plans for Any Trip

Introduction You land after a long flight. Your phone shows no signal. The airport SIM queue wraps around the corner. Your home carrier wants a huge fee…

Read More

Mobile Network Codes by Country: Complete Global Guide

Introduction Your phone connects to a cell tower in seconds. You never see the hidden work behind that link. Every SIM card carries secret digital ID numbers….

Read More

Restaurant Reservation Software Guide: Online Bookings, Live Table Management, and Front-of-House Control

Introduction Friday night arrives, and the dining room fills fast. Phones ring nonstop. Guests wait at the door. A paper notebook fills with crossed-out names and rushed…

Read More

Real-Time DataOps Monitoring Case Studies That Protect Your Business Dashboards

Introduction Imagine walking into a Monday morning team meeting ready to share great sales numbers. Suddenly, your manager points out that the revenue chart dropped to zero…

Read More

Unifying Data Culture Across Sales, Marketing, and Engineering

Different departments in your company often collect data in separate, isolated silos. Marketing tracks customer clicks in one tool, while sales logs deals in another spreadsheet. When…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x