
- 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
HTML form action and onsubmit validations with JavaScript?
Let’s see an example wherein we are validating input text onsubmit −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <form onsubmit="return validateTheForm()" method="GET" action="https://mail.google.com/mail/"> <br/> <input type="text" id="txtInput" value=""/> <input type="submit" value="search"/> </form> <script> function validateTheForm(){ var validation = (document.getElementById('txtInput').value == 'gmail'); if(!validation){ alert('Something went wrong...Plese write gmail intext box and click'); return false; } return true; } </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
Step 1
Type GMAIL in text box −
After clicking the search button, the output is as follows −
Step 2
On typing another value, except GMAIL, you will get an alert message.
Let’s first type the value and press enter −
After clicking the search button, you will get an alert message as in the below output −
- Related Articles
- HTML Form action Attribute
- HTML DOM Form action Property
- HTML onsubmit Event Attribute
- What is onsubmit event in JavaScript?
- How to find the action attribute and method of a form in JavaScript?
- Text Validations in Cypress
- How to call a JavaScript function from an onsubmit event?
- Getting HTML form values and display on console in JavaScript?
- How to create a signup form with HTML and CSS?
- How to submit an HTML form using JavaScript?
- How to use Validations in Laravel?
- Client side validation with HTML and without JavaScript
- Remove and add new HTML Tags with JavaScript?
- Use HTML with jQuery to make a form
- How to clear HTML form fields with jQuery?

Advertisements