Yngvarr

Generation

regexFri, 24 Feb 2023

валидация текста в форме

<form action=""> <label for=""> Введите строку: <input type="text" name="str" value=""> </label> <button>Отправить</button> </form> <script> let input = document.querySelector('input'); let form = document.querySelector('form'); form.addEventListener('submit', (e) => { let value = input.value; let patt = /\w+/; if (patt.test(value)) { alert('Все ок'); } else { e.preventDefault(); // останавливаем отправку формы input.style.border = '2px solid red'; alert('Введите строку'); }

Questions about programming?Chat with your personal AI assistant