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>
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);
});
โ 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">
Your backend receives:
{
"amount": "100000"
}
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})?"ormaxlength="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)