Home >Web Front-end >Front-end Q&A >How to set up a form in javascript

How to set up a form in javascript

PHPz
PHPzOriginal
2023-04-21 09:06:34752browse

With the development of Web technology and its application in more and more scenarios, forms have become one of the indispensable elements in Web development. Forms can be used to collect user-entered data, which is very important to many web applications. When the user fills out the form and submits it, we need to process this data. In this case, JavaScript plays a key role.

This article will discuss how to set up forms using JavaScript and explore some common tips and techniques.

Basic Form

First, let’s take a look at a basic HTML form. The following is the HTML code of a simple form:

<form>
  <label for="name">姓名:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">邮箱:</label>
  <input type="email" id="email" name="email" required><br><br>

  <label for="age">年龄:</label>
  <input type="number" id="age" name="age" required><br><br>

  <input type="submit" value="提交">
</form>

The above form contains three required fields: name, email address and age, as well as a submit button. If one of the items is not filled in, a prompt box will pop up to prompt the user to fill in the item.

Setting the form value

Setting the value of the form using JavaScript can be done in the following ways:

document.getElementById("name").value = "John";

The above code sets "John" as the default value for the name. We can also set the value in the form dynamically this way:

document.getElementById("name").value = userName;

In this case, "userName" is a variable whose value can be obtained in other ways, such as from a database or other web service Obtain.

Get the form value

You can get the value of the form through the following code:

let nameValue = document.getElementById("name").value;
let emailValue = document.getElementById("email").value;
let ageValue = document.getElementById("age").value;

Here the values ​​in the form are stored in three variables. This can be used conveniently when processing form data.

Form Validation

Form validation is an important part that helps protect the website from attacks from malicious users and ensures the validity and accuracy of form data.

The following are some common form validation techniques:

  1. Required field validation: In forms, required fields often require mandatory validation. For example, in the example above, name, email, and age are all required fields. To ensure that the user has filled in the required fields, we use the "required" attribute. If the user submits a form without completing required fields, the browser will warn the user before the submission fails.
  2. Email Validation: In many web applications, we need to ensure that the email address entered by the user is valid. To validate email addresses we use regular expressions.

Here is an example of JavaScript validating an email address:

function validateEmail(email) {
  var re = /\S+@\S+\.\S+/;
  return re.test(email);
}

In the above code, we have used a simple regular expression that validates the email address format.

  1. Form submission: When the form receives a submission request, we need to validate the form data. This can be achieved by setting the "onsubmit" event on the submit button.

The following is the JavaScript code that associates the validation function with the form submission event:

document.getElementById("myForm").onsubmit = function() {
  if (!validateForm()) {
    return false;
  }
};

The above code will validate the form before it is submitted. If validation fails, the form will not submit.

  1. Prompt message: When the data in the form is invalid, we usually need to display a prompt message about the problem to the user. This can be achieved by adding elements to the form, for example by adding a
    element called "errorText".

Here is a JavaScript code that will display an error message to the user when the form validation fails:

function showErrorMessage(field, message) {
  var errorText = document.createElement("div");
  errorText.className = "error";
  errorText.innerHTML = message;
  field.parentNode.insertBefore(errorText, field.nextSibling);
}

In the above code, we have used JavaScript to create a file called

element of "errorText" and insert it before the next sibling element of the failing form field.

Summary

In this article, we briefly introduced the function of JavaScript in setting up forms, proposed some common form validation techniques, and how to validate and publish prompts before form submission. information. Using these technologies, we can implement more advanced interactions and data processing in our form applications, ensuring the security and validity of user input data.

The above is the detailed content of How to set up a form in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn