**XpressCalc ** is a lightweight JavaScript library that enables Excel-like calculations in HTML input fields. It supports numeric inputs, arithmetic operations, and real-time validation.
- ✅ Supports standard arithmetic operations (
+
,-
,*
,/
,()
). - ✅ Allows formulas to begin with
=
like Excel. - ✅ Live input sanitization to prevent invalid characters.
- ✅ Rounds results to 2 decimal places.
- ✅ Highlights input errors and alerts users.
- ✅ Lightweight and dependency-free.
Include the script in your HTML file:
<script src="path/to/XpressCalc.js"></script>
Or, install via npm:
npm install XpressCalc
Ensure your inputs are wrapped inside a container with id="calc-zone"
and have the attribute data-class="Currency"
.
<div id="calc-zone">
<input type="text" data-class="Currency" placeholder="Enter formula or number">
<input type="text" data-class="Currency" placeholder="Enter another value">
</div>
The library will automatically process the input on focusout
:
XpressCalc .attachEventListeners();
Input | Valid? | Result |
---|---|---|
=10+20 |
✅ Yes | 30 |
=100/2+5 |
✅ Yes | 55 |
50 |
✅ Yes | 50 |
=10*-5 |
❌ No | Error (Negative values not allowed) |
abc123 |
❌ No | Error |
Manually process an input field.
const input = document.querySelector('input');
XpressCalc.processSingleInput(input);
Prevents invalid characters from being entered.
document.querySelector('input').addEventListener('keydown', XpressCalc.validateKeypress.bind(XpressCalc));
Applies error styling and focus.
XpressCalc.setError(document.querySelector('input'), 'Invalid input!');
Removes error styling.
XpressCalc.clearError(document.querySelector('input'));
Modify styles for error highlighting:
input.error {
border: 2px solid red;
background-color: #ffe6e6;
}
This project is licensed under the MIT License - see the LICENSE file for details.
Gabriel A. - Built with ❤️
Give a ⭐ if you find this useful!