JavaScript로 빈 입력 필드에 대한 양식 유효성 검사를 추가하려면 코드는 다음과 같습니다. -
예시
<!DOCTYPE html> <html> <head></head> <h1>JavaScript empty input field validation example</h1> <form name="Form1" onsubmit="return emptyValidation()" required> Name: <input type="text" name="firstName" /> <input type="submit" value="Submit" /> </form> <h2>Submit the form empty to see the validation performed</h2> <script> function emptyValidation() { var formField = document.forms["Form1"]["firstName"].value; if (formField == "" || x == null) { alert("Name must be filled out"); return false; } } </script> </body> </html>
출력
위의 코드는 다음과 같은 출력을 생성합니다 -
비어 있는 양식 제출 시 -