


Use native javascript to create universal form validation - sharper use of dom objects_javascript skills
First of all, let’s take a look at the effect. It’s nothing special, haha!
The code called by
is quite simple. There is no need to create other Label or span tags. The script will be automatically generated:
Next, let’s take a look at the checkResult function. The checkCondition parameter represents the judgment condition. When the condition is true, the prompt message is displayed; The showAfterId parameter is the element ID before the created label that displays the prompt information. Here we create a span after the input to display the prompt information, so the parameter value passed in is the ID "txt1" of the current input; the last parameter is displayed Text, there is no need to be verbose about this.
//Verification cannot be empty and display prompt information
function checkResult(checkCondition, showAfterId, showMsg) {
var showLabelId = showAfterId "showMsg";
if (checkCondition) {
if (document.getElementById(showLabelId)) {
document.getElementById(showLabelId ).innerHTML = showMsg;
} else {
createShowElement(showAfterId, showLabelId, "color:red", showMsg, 'span');
}
} else if (!checkCondition) {
if (document.getElementById(showLabelId))
document.getElementById(showLabelId).innerHTML = '';
}
}
Okay, finally let’s look at this "createShowElement(currentId, elementId, style, showMsg, tagName)" function: currentId is the ID of the current tag; elementId is the ID of the created tag; style is the style of the created tag, just follow the writing method of the style; showMsg will not be discussed ;tagName is the name of the created tag, such as label or span, etc.
//Create a dom
function that displays prompt information createShowElement(currentId, elementId, style, showMsg, tagName) {
if (!tagName) tagName = 'label';
var currentDom = document.getElementById(currentId);
var showMsgDom = document.createElement( tagName);
//showMsgDom.setAttribute("style", "color:" textColor ";");
if (style)
showMsgDom.setAttribute("style", style);
showMsgDom.setAttribute("id", elementId);
showMsgDom.innerHTML = showMsg;
currentDom.parentNode.insertBefore(showMsgDom, currentDom.nextSibling);
}
Only For communication, everyone is welcome to give advice and eager to provide valuable opinions. Personally, I feel that even when writing a simple script verification program, we should try our best to follow object-oriented thinking and pursue a harmonious point in scalability and efficiency, which will not affect efficiency, and at the same time make any program we write more efficient. Scalability, this idea is actually not difficult, but it is often ignored by many junior programmers.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use Flask-WTF to implement form validation Flask-WTF is a Flask extension for handling web form validation. It provides a concise and flexible way to validate user-submitted data. This article will show you how to use the Flask-WTF extension to implement form validation. Install Flask-WTF To use Flask-WTF, you first need to install it. You can use the pip command to install: pipinstallFlask-WTF import the required modules in F

Laravel is a popular PHP web development framework that provides many convenient features to speed up the work of developers. Among them, LaravelValidation is a very practical function that can help us easily validate form requests and user-entered data. This article will introduce how to use LaravelValidation to validate form requests. What is LaravelValidationLaravelValidation is La

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

How to use middleware to handle form validation in Laravel, specific code examples are required Introduction: Form validation is a very common task in Laravel. In order to ensure the validity and security of the data entered by users, we usually verify the data submitted in the form. Laravel provides a convenient form validation function and also supports the use of middleware to handle form validation. This article will introduce in detail how to use middleware to handle form validation in Laravel and provide specific code examples.

PHP form validation tips: How to use the filter_input function to verify user input Introduction: When developing web applications, forms are an important tool for interacting with users. Correctly validating user input is one of the key steps to ensure data integrity and security. PHP provides the filter_input function, which can easily verify and filter user input. This article will introduce how to use the filter_input function to verify user input and provide relevant code examples. one,

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

ThinkPHP6 is a PHP-based MVC framework that greatly simplifies the development of web applications. Among them, form validation is a very basic and important function. In this article, we will introduce how to perform form validation operations in ThinkPHP6. 1. Definition of validation rules In ThinkPHP6, validation rules need to be defined in the controller. We can define the rules by defining a $validate attribute in the controller, as shown below: usethinkVa
