screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <h3>Javascript Add Space Every 4 characters</h3> <input type="text" id="inputField" oninput="addSpaces()"> <script> function addSpaces() { let inputField = document.getElementById("inputField"); let inputValue = inputField.value.replace(/\s/g, ''); // Remove existing spaces let newValue = ''; for (let i = 0; i < inputValue.length; i++) { newValue += inputValue[i]; if ((i + 1) % 4 === 0) { newValue += ' '; // Add space every 4 characters } } inputField.value = newValue.trim(); // Trim spaces from both ends } </script> <style> input { padding: 10px; font-size: 16px; } </style> </body> </html>