
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to add form validation for empty input fields with JavaScript?
To add form validation for empty input fields with JavaScript, the code is as follows −
Example
<!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>
Output
The above code will produce the following output −
On submitting the form empty −
- Related Articles
- MySQL group_concat to add a separator for empty fields?
- Style Input Fields of a form with CSS
- How to create a password validation form with CSS and JavaScript?
- Some Lesser-Known CSS Properties for Form Input Fields
- How to do basic form validation using JavaScript?
- Allow a label to be used for form validation with Bootstrap
- Add a background color to the form input with CSS
- How to clear HTML form fields with jQuery?
- Add multiple number input fields with JOptionPane and display the sum in Console with Java
- How to add validation to your Django project?
- Container for form input and label with Bootstrap
- Set PIN validation with JavaScript?
- How to check input file is empty or not using JavaScript/jQuery?
- Bootstrap complex form layout for combined vertical/inline fields with HTML
- Form validation using Django

Advertisements