hi,
we have a validation like allow only number and "," as per functionality.
and this has to work on click of save button only.
used Number.isnan but its not validating the +1 kind of input since internally treating as number.
help me on this
hi,
we have a validation like allow only number and "," as per functionality.
and this has to work on click of save button only.
used Number.isnan but its not validating the +1 kind of input since internally treating as number.
help me on this
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Gowtham CpRank #1,552 · 1K reputationPosted Sep 1, 2024, 10:16 PM EDT
Hi ,
To ensure the input only contains numbers and commas when clicking the "Save" button, use a regular expression to validate the input. Here's how:
document.getElementById('saveButton').addEventListener('click', function() { const input = document.getElementById('inputField').value.trim(); const isValid = /^[0-9,]+$/.test(input); if (isValid) { console.log('Valid input:', input); // Proceed with save } else { console.log('Invalid input! Only numbers and commas are allowed.'); // Show error message } });Hope it helps!
Murali PRank #6,159 · 275 reputationPosted Sep 1, 2024, 11:39 PM EDT
Thanks a lot goutham for ur help... It saves my day
Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 1, 2024, 11:42 PM EDT
If you'd like to validate the input without using regular expressions, you can take a more manual approach by checking each character in the input string to ensure it’s either a digit or a comma.
Here’s how you can do this:
Save document.getElementById("saveButton").addEventListener("click", function () { const inputValue = document.getElementById("inputField").value; // Validate each character manually const isValid = validateInput(inputValue); const errorMessageElement = document.getElementById("errorMessage"); if (!isValid) { errorMessageElement.textContent = "Please enter only numbers and commas."; } else { errorMessageElement.textContent = ""; // Clear error message // Continue with your save functionality here alert("Input is valid!"); } }); function validateInput(input) { for (let i = 0; i = '0' && char