Home > Web Front-end > JS Tutorial > JavaScript function form validation: ensure the validity of user input

JavaScript function form validation: ensure the validity of user input

WBOY
Release: 2023-11-18 08:08:05
Original
684 people have browsed it

JavaScript function form validation: ensure the validity of user input

JavaScript function form validation: ensuring the validity of user input

When users enter data in web forms, we must ensure the validity of these input data to ensure Provide a good user experience and data security. JavaScript function form validation is a commonly used method that can be used to verify whether the data entered by the user meets the requirements. Next, we'll cover some common form validation methods and provide specific code examples.

  1. Non-null validation

Non-null validation is the most basic form validation function. Before submitting the form, we need to make sure the user has a value in the required input box. The following is a simple non-empty validation function:

function validateRequired(input){
  if(input.value == ""){
    input.classList.add("error"); // 添加错误样式
    return false;
  } else {
    input.classList.remove("error"); // 移除错误样式
    return true;
  }
}
Copy after login

In HTML, we can use the onsubmit event to trigger validation:

<form onsubmit="return validateForm()">
  <input type="text" id="name" required>
  <button type="submit">提交</button>
</form>
Copy after login
  1. Email verification

Email verification is one of the common form verification functions. We can use regular expressions to verify whether the email address entered by the user meets the specification:

function validateEmail(input){
  var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/; // 邮箱正则表达式
  if(!emailRegex.test(input.value)){
    input.classList.add("error");
    return false;
  } else {
    input.classList.remove("error");
    return true;
  }
}
Copy after login

Used in HTML:

<form onsubmit="return validateForm()">
  <input type="email" id="email" required>
  <button type="submit">提交</button>
</form>
Copy after login
  1. Password verification

Password verification is an important part of ensuring the security of user accounts. We can judge the strength of a password by verifying its length, including uppercase and lowercase letters and numbers:

function validatePassword(input){
  var passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*d)[a-zA-Zd]{8,}$/; // 密码正则表达式(至少8个字符,包含大小写字母和数字)
  if(!passwordRegex.test(input.value)){
    input.classList.add("error");
    return false;
  } else {
    input.classList.remove("error");
    return true;
  }
}
Copy after login

Usage in HTML:

<form onsubmit="return validateForm()">
  <input type="password" id="password" required>
  <button type="submit">提交</button>
</form>
Copy after login
  1. Number verification

Sometimes, we need to verify whether the user input is a number. You can use the built-in isNaN() function for verification:

function validateNumber(input){
  if(isNaN(input.value)){
    input.classList.add("error");
    return false;
  } else {
    input.classList.remove("error");
    return true;
  }
}
Copy after login

Usage in HTML:

<form onsubmit="return validateForm()">
  <input type="number" id="age" required>
  <button type="submit">提交</button>
</form>
Copy after login

The above are some common form verification methods, we can use them according to actual Combine and expand as needed. Through JavaScript function form validation, we can ensure the validity of data entered by users and improve user experience and data security.

The above is the detailed content of JavaScript function form validation: ensure the validity of user input. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template