DEV Community

Akshay Joshi
Akshay Joshi

Posted on

๐Ÿ’ฐ Real-Time Indian Rupee Formatting in HTML Forms โ€” With Clean Backend Submissions

Ever typed 1000000 in a payment form and wished it magically became 10,00,000?
Now imagine you get the visual formatting, but the backend receives a clean, unformatted number.

Thatโ€™s exactly what this post covers โ€” real-time Indian currency formatting in vanilla JavaScript, with masked display and raw input submission.


๐ŸŽฏ What Weโ€™re Building

A form field that:

  • Shows Indian-style commas (1,00,000) while typing
  • Saves a clean numeric value (100000) to the backend
  • Requires no external libraries
  • Is simple and production-safe

๐Ÿ’ก Why Not Just Use <input type="number">?

Because:

  • HTML5 number inputs donโ€™t support comma formatting
  • Indian numbering system (12,34,567) isnโ€™t natively supported
  • Input masks like Cleave.js donโ€™t store raw values unless explicitly managed

So we go native โ€” and clean.


๐Ÿ”ง Live Demo Structure

<form id="paymentForm">
  <label for="formattedAmount">Amount (โ‚น):</label>
  <input type="text" id="formattedAmount" placeholder="Enter amount" inputmode="numeric">
  <input type="hidden" name="amount" id="rawAmount">

  <button type="submit">Submit</button>
</form>
Enter fullscreen mode Exit fullscreen mode

We use:

  • #formattedAmount: visible input with commas
  • #rawAmount: hidden input storing plain numeric value

๐Ÿง  JavaScript Logic

const formattedInput = document.getElementById('formattedAmount');
const rawInput = document.getElementById('rawAmount');

function formatIndianNumber(x) {
  const parts = x.split('.');
  let integerPart = parts[0];
  const decimalPart = parts.length > 1 ? '.' + parts[1] : '';

  integerPart = integerPart.replace(/^0+/, '') || '0';

  const lastThree = integerPart.slice(-3);
  const rest = integerPart.slice(0, -3);
  const formatted = rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + (rest ? ',' : '') + lastThree;

  return formatted + decimalPart;
}

formattedInput.addEventListener('input', () => {
  const raw = formattedInput.value.replace(/,/g, '').replace(/[^\d.]/g, '');
  formattedInput.value = formatIndianNumber(raw);
  rawInput.value = raw;
});

document.getElementById('paymentForm').addEventListener('submit', () => {
  rawInput.value = formattedInput.value.replace(/,/g, '');
  console.log("Submitted value:", rawInput.value);
});
Enter fullscreen mode Exit fullscreen mode

โœ… Features

Feature Included
Live Indian formatting โœ…
Works with decimals โœ… (basic)
Form-safe hidden input โœ…
No external dependency โœ…
Easy integration โœ…

๐Ÿ“ฆ Backend Ready

At submission:

<input type="hidden" name="amount" value="100000">
Enter fullscreen mode Exit fullscreen mode

Your backend receives:

{
  "amount": "100000"
}
Enter fullscreen mode Exit fullscreen mode

No commas, no parsing required. Just clean data.


๐Ÿ›ก๏ธ Bonus Tips

  • Don't use type="number" on the formatted input โ€” it conflicts with commas.
  • Add pattern="\d+(,\d{2})?" or maxlength="15" for validation.
  • Support for decimals? Adjust regex and formatting as needed.

๐Ÿš€ Final Thoughts

This pattern is a small but powerful UX booster:

  • Keeps forms user-friendly
  • Simplifies backend parsing
  • Gives you full control with native JavaScript

Sometimes, the cleanest solutions donโ€™t need packages โ€” just precise logic.


Happy coding ๐Ÿ‡ฎ๐Ÿ‡ณ๐Ÿ’ป

Top comments (0)