<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sharjeel Sultan</title>
    <description>The latest articles on DEV Community by Sharjeel Sultan (@sharjeel_sultan).</description>
    <link>https://dev.to/sharjeel_sultan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F974686%2F32dcae4c-9fef-441d-b1c1-82c103883fca.jpeg</url>
      <title>DEV Community: Sharjeel Sultan</title>
      <link>https://dev.to/sharjeel_sultan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sharjeel_sultan"/>
    <language>en</language>
    <item>
      <title>How to Make Input Element with Text Type to Take the Number as Text Perfectly</title>
      <dc:creator>Sharjeel Sultan</dc:creator>
      <pubDate>Fri, 05 Sep 2025 05:56:23 +0000</pubDate>
      <link>https://dev.to/sharjeel_sultan/how-to-make-input-element-with-text-type-to-take-the-number-as-text-perfectly-262p</link>
      <guid>https://dev.to/sharjeel_sultan/how-to-make-input-element-with-text-type-to-take-the-number-as-text-perfectly-262p</guid>
      <description>&lt;p&gt;HTML forms are the backbone of user input across the web. One common task? Accepting only numbers in an input field. Naturally, you'd reach for the built-in , expecting it to solve your problems. Unfortunately, it brings along some pesky issues like the infamous up/down spinners, unexpected character entries, and more.&lt;/p&gt;

&lt;p&gt;If you've been struggling with native HTML input types and regex limitations, you’re not alone. In this guide, we’ll explore these issues and explain how a custom keydown function can solve them once and for all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Input Field Types in HTML
&lt;/h2&gt;

&lt;p&gt;HTML offers various types of input fields, each with their own use case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type="text" //Accepts any character
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type="number" //Intended for numeric input only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, HTML5 validation isn’t perfect. Each has its quirks, and developers often choose one over the other based on the desired behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why type="number" Can Be Problematic
&lt;/h2&gt;

&lt;p&gt;Here are the major issues developers face when using type="number":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spinner Buttons Appear&lt;/strong&gt;: Especially on desktop browsers, the input includes increment/decrement controls, which ruin UI aesthetics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Character Overflow&lt;/strong&gt;: Even if you set min or max, users can still type numbers of any length (like 9999999999999999).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Numeric Input on Paste&lt;/strong&gt;: Users can paste anything into the field, completely bypassing validation until submission.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcywm57m4mk8jrt3w3ovw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcywm57m4mk8jrt3w3ovw.png" alt="code snippet - problem with type number in input" width="800" height="193"&gt;&lt;/a&gt; &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxz4g3h5bzc2cx2th2kcl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxz4g3h5bzc2cx2th2kcl.png" alt="Character Overflow problem with type number in input" width="683" height="276"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Common Issues with type="text" and Regex-Based Validation
&lt;/h2&gt;

&lt;p&gt;You might think using type="text" with a regex pattern can help:&lt;br&gt;
&lt;code&gt;&amp;lt;input type="text" pattern="\d*"&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
But that leads to usability problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users can still enter letters unless you validate after input.&lt;/li&gt;
&lt;li&gt;Regex restrictions block essential keys like backspace, delete, or paste.&lt;/li&gt;
&lt;li&gt;Mobile behavior gets weird, you lose access to numeric keyboards.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Limitations of Built-in HTML Validation
&lt;/h2&gt;

&lt;p&gt;Even with required, min, max, and pattern, built-in HTML form validation can be unreliable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;min/max&lt;/strong&gt; Doesn’t restrict input length&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pattern&lt;/strong&gt; Only triggers on form submission&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input type&lt;/strong&gt; Doesn’t fully prevent bad input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fheah7qgezyzibnq93zb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fheah7qgezyzibnq93zb7.png" alt="code-snippet - problem with input type text" width="800" height="239"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3z9umg6lvr89gxh5e5y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3z9umg6lvr89gxh5e5y.png" alt="min max failure while inserting form" width="669" height="264"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Custom JavaScript as a Solution
&lt;/h2&gt;

&lt;p&gt;To overcome these limitations, JavaScript is the best route. By binding a function to the onkeydown event, you can &lt;strong&gt;control what gets typed in real-time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This allows you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filter invalid characters before they appear&lt;/li&gt;
&lt;li&gt;Allow important keys like delete, backspace, arrows&lt;/li&gt;
&lt;li&gt;Maintain a seamless user experience&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Introducing the key function for Input Restriction
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const accountNumberConstraint = (event) =&amp;gt; {
  const allowedKeys = [
    "Backspace", "Delete", "ArrowLeft", "ArrowRight", "Tab",
    "Enter", "Home", "End"
  ];

  if (
    !/^[0-9]$/.test(event.key) &amp;amp;&amp;amp;      // 1️⃣ block non-digits
    !allowedKeys.includes(event.key) &amp;amp;&amp;amp; // 2️⃣ allow navigation/editing keys
    !(event.ctrlKey || event.metaKey)   // 3️⃣ allow ctrl/meta shortcuts (copy, paste, undo)
  ) {
    event.preventDefault();             // 🚫 block everything else
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lets break down the working of our special function:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checks if the key pressed is a digit (0–9)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uses a regex:&lt;/strong&gt; &lt;code&gt;/^[0-9]$/&lt;/code&gt; → allows only single numeric characters.&lt;/li&gt;
&lt;li&gt;Example: "5" ✅, "a" ❌.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Allows essential control keys (from allowedKeys)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backspace/Delete → for editing.&lt;/li&gt;
&lt;li&gt;Arrow keys → for moving cursor.&lt;/li&gt;
&lt;li&gt;Tab/Enter → navigation or submit.&lt;/li&gt;
&lt;li&gt;Home/End → quick cursor moves.&lt;/li&gt;
&lt;li&gt;Allows system shortcuts (Ctrl/Meta)&lt;/li&gt;
&lt;li&gt;(event.ctrlKey || event.metaKey) ensures combinations like:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Ctrl+C / Cmd+C (copy)&lt;/li&gt;
&lt;li&gt;Ctrl+V / Cmd+V (paste)&lt;/li&gt;
&lt;li&gt;Ctrl+Z / Cmd+Z (undo)&lt;/li&gt;
&lt;li&gt;Ctrl+A / Cmd+A (select all)&lt;/li&gt;
&lt;li&gt;Prevents everything else&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If none of the above checks pass → &lt;code&gt;event.preventDefault(&lt;/code&gt;) stops the character from appearing in the input.&lt;/p&gt;
&lt;h2&gt;
  
  
  Practical Usage with &lt;code&gt;maxLength&lt;/code&gt;, &lt;code&gt;onChange&lt;/code&gt;, and the Function passed to &lt;code&gt;onKeyDown&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In real-world React (or JSX) applications, the numeric-only constraint isn’t used in isolation. It often needs to work alongside other input attributes and handlers. For example, setting a maximum length, updating state, and ensuring clean validation.&lt;br&gt;
Now, when special function passed to &lt;code&gt;onkeydown&lt;/code&gt; is the game-changer. This function filters keystrokes with precision&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input
  type="text"
  placeholder="Enter Account No."
  className="inputcc"
  maxLength={10}
  value={inputData.account_no}
  onChange={(e) =&amp;gt;
    setInputData({ ...inputData, account_no: e.target.value })
  }
  onKeyDown={(event) =&amp;gt; accountNumberConstraint(event)}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's what it does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;type="text"&lt;/code&gt;&lt;/strong&gt; → Keeps the field flexible but avoids native number input quirks (like spinners).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;placeholder="Enter Account No."&lt;/code&gt;&lt;/strong&gt; → Provides user guidance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;className="inputcc"&lt;/code&gt;&lt;/strong&gt; → Lets you style the field with CSS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;maxLength={10}&lt;/code&gt;&lt;/strong&gt; → Ensures users can’t enter more than 10 characters, which is crucial for account numbers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;value={inputData.account_no}&lt;/code&gt;&lt;/strong&gt; → Makes the input a controlled component, keeping React in charge of the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;onChange={(e) =&amp;gt; setInputData(...)}&lt;/code&gt;&lt;/strong&gt; → Updates state on every valid change, ensuring React always has the latest value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;onKeyDown={(event) =&amp;gt; accountNumberConstraint(event)}&lt;/code&gt;&lt;/strong&gt; → Invokes your special function to filter keystrokes in real-time, only allowing numeric entries while still permitting actions like backspace, delete, and arrow navigation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvhk7hd69eatmupjf7ok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvhk7hd69eatmupjf7ok.png" alt="input code snippet with use on onKeyDown event handler" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1yrrk52ap4hxsvtgxhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1yrrk52ap4hxsvtgxhh.png" alt="form working as expected" width="672" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the end of the day, the perfect solution came from mixing the best of both worlds, using &lt;code&gt;type="text"&lt;/code&gt; for flexibility, adding &lt;code&gt;maxLength&lt;/code&gt; for strict digit limits, and powering it all with the custom &lt;em&gt;NumberConstraint function&lt;/em&gt; bound to &lt;code&gt;onKeyDown&lt;/code&gt;. Together, they create a smooth, user-friendly input that accepts only valid numbers without the quirks of &lt;code&gt;type="number"&lt;/code&gt; or the frustrations of regex-only restrictions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>All you need to know about React, SPA and Virtual Dom</title>
      <dc:creator>Sharjeel Sultan</dc:creator>
      <pubDate>Fri, 18 Nov 2022 05:57:50 +0000</pubDate>
      <link>https://dev.to/sharjeel_sultan/all-you-need-to-know-to-get-started-with-react-36d3</link>
      <guid>https://dev.to/sharjeel_sultan/all-you-need-to-know-to-get-started-with-react-36d3</guid>
      <description>&lt;h2&gt;
  
  
  What is React
&lt;/h2&gt;

&lt;p&gt;React is JavaScript Library which helps to build User Interface using components. &lt;strong&gt;Components&lt;/strong&gt; are isolated, reusable pieces of code which works similar to JavaScript functions. Each component renders and appears on front end as a visible element. The whole application in React is rendered into a &lt;strong&gt;SPA&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is SPA and how it differs from MPA
&lt;/h2&gt;

&lt;p&gt;SPA stands for &lt;strong&gt;single page application&lt;/strong&gt;, These days the web development crowd is getting more and more into the Single-Page Application. SPA enables you to work inside a browser while eliminating the need to reload a page when the user is using it. Since SPA eliminates the need to reload a page, maintains minimum possible code, improves user experience and app performance, these feature of SPA makes it stand out from MPA. Facebook, Twitter, as well as Gmail, Google Maps, and Google Drive all of them are examples of Single Page Application which we encounter daily in our lives.&lt;br&gt;&lt;br&gt;
On the other hand MPA, a &lt;strong&gt;Multiple page application&lt;/strong&gt; is the conventional way of app development. MPA reloads each time the user opens a new page in the browser. The major con of MPA is Performance speed — every time a user refreshes a page, the browser has to display all the data from scratch. Apart from SPA, Multiple page application has its own importance and it is used in building complex websites. The major example of MPA is Amazon — whenever you request new content, the page has to reload all over again. This is considered conventional architecture, but it’s still highly valuable and is used with great results.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key concept of React
&lt;/h2&gt;

&lt;p&gt;We have talked about what React is, now prior to dive deeper into how it actually works. you must be aware of what is &lt;strong&gt;DOM&lt;/strong&gt; -  &lt;strong&gt;Document Object Model&lt;/strong&gt; it is the structural representation of all nodes in an HTML document. The DOM represents the web page often called a document with a logical tree and each  branch of the tree ends in a node and each node contains object programmers can modify the content of the document. Now you need to know about Virtual DOM and how it plays an important role in React Application&lt;/p&gt;

&lt;h2&gt;
  
  
  Virtual DOM
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;virtual DOM (VDOM)&lt;/strong&gt; is a programming concept where an ideal, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM by a library such as &lt;em&gt;ReactDOM&lt;/em&gt;. This process is called &lt;em&gt;reconciliation&lt;/em&gt;.&lt;br&gt;
As a result of this approach, React implements its declarative API by ensuring the DOM matches the state you specify in the UI. This abstracts out the attribute manipulation, event handling, and manual DOM updating that you would otherwise have to use to build your app.&lt;br&gt;
The term "virtual DOM" is often used differently since it refers to a pattern rather than a specific technology. React, however, also uses internal objects called “fibers” to hold additional information about the component tree. They may also be considered a part of “virtual DOM” implementation in React.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
