A web form has a client-server relationship. They are used to send data handled by a web server for processing and storage. The form itself is the client, and the server is any storage mechanism that can be used to store, retrieve and send data when needed. This guide will teach you how to create a web form with Next.js. HTML forms are built using the tag. It takes a set of attributes and fields to structure the form for features like text fields, checkboxes, dropdown menus, buttons, radio buttons, etc.Here's the syntax of an HTML form: The front-end looks like this: The HTML tag acts as a container for different
A process that checks if the information provided by a user is correct or not. Form validation also ensures that the provided information is in the correct format (e.g. there's an @ in the email field). These are of two types:
Though both of these types are equally important, this guide will focus on client-side validation only. Client-side validation is further categorized as:
So, a form using this attributes may look like: With these validation checks in place, when a user tries to submit an empty field for Name, it gives an error that pops right in the form field. Similarly, a roll number can only be entered if it's 10-20 characters long. Form Validation is important to ensure that a user has submitted the correct data, in a correct format. JavaScript offers an additional level of validation along with HTML native form attributes on the client side. Developers generally prefer validating form data through JavaScript because its data processing is faster when compared to server-side validation, however front-end validation may be less secure in some scenarios as a malicious user could always send malformed data to your server. The following example shows using JavaScript to validate a form:
The HTML script tag is used to embed any client-side JavaScript. It can either contain inline scripting statements (as shown in the example above) or point to an external script file via the 9 attribute. This example validates the name and roll number of a user. The 0 function does not allow an empty name field, and the roll number must be at least three digits long. The validation is performed when you hit the Submit button. You are not redirected to the next page until the given values are correct.JavaScript validation with Regular Expressions uses the 4 HTML attribute. A regular expression (commonly known as RegEx) is an object that describes a pattern of characters. You can only apply the 4 attribute to the element. This way, you can validate the input value using Regular Expressions (RegEx) by defining your own rules. Once again, if the value does not match the defined pattern, the input will give an error. The below example shows using the 4 attribute on an 5 element:
The password form field must only contain digits (0 to 9), lowercase alphabets (a to z) and it must be no more than 15 characters in length. No other characters (#,$,&, etc.) are allowed. The rule in RegEx is written as 6.
In the following section you will be creating forms in React using Next.js. Create a new Next.js app. You can use the for a quick start. In your command line terminal, run the following:
Answer the questions to create your project, and give it a name, this example uses 7. Next 8 into this directory, and run 9 or 0 command to start the development server.Open the URL printed in the terminal to ensure that your app is running successfully. Both the client and the server will be built using Next.js. For the server part, create an API endpoint where you will send the form data. Next.js offers a file-based system for routing that's built on the concept of pages. Any file inside the folder 1 is mapped to 2 and will be treated as an API endpoint instead of a page. This API endpoint is going to be server-side only.Go to 1, create a file called 4 and paste this code written in Node.js:This form 5 function will receive the request 6 from the client (i.e. submitted form data). And in return, it'll send a response 7 as JSON that will have both the first and the last name. You can access this API endpoint at 8 or replace the localhost URL with an actual Vercel deployment when you deploy.
You can now use 9 relative endpoint inside the action attribute of the form. You are sending form data to the server when the form is submitted via 2 HTTP method (which is used to send data).
If you submit this form, it will submit the data to the forms API endpoint 9. The server then responds, generally handling the data and loading the URL defined by the action attribute, causing a new page load. So in this case you'll be redirected to 8 with the following response from the server.You have created a Next.js API Route for form submission. Now it's time to configure the client (the form itself) inside Next.js using React. The first step will be extending your knowledge of HTML forms and converting it into React (using JSX). Here's the same form in a React function component written using JSX.
Here's what changed:
This completes the basic structure of your Next.js-based form.
JavaScript brings interactivity to our web applications, but sometimes you need to control the JavaScript bundle from being too large, or your sites visitors might have JavaScript disabled. There are several reasons why users disable JavaScript:
Regardless of the reason, disabling JavaScript will impact site functionality partially, if not completely. Next open the 7 directory. Inside the 1 directory, create a file 2.
Let's use the same code from above:
With JavaScript disabled, when you hit the Submit button, an event is triggered, which collects the form data and sends it to our forms API endpoint as defined in the 2 HTTP 0. You'll be redirected to the 9 endpoint since that's how form action works.The form data will be submitted on the server as a request 6 to the form handler function written above. It will process the data and return a JSON string as a response 7 with your submitted name included.
Inside Now, as soon as the form is submitted, we prevent the form's default behavior of reloading the page. We'll take the form data, convert it to JSON string, and send it to our server, the API endpoint. Finally, our server will respond with the name submitted. All of this with a basic JavaScript |