


Universal validation library for front-end data based on jQuery_jquery
So I slowly began to summarize the previous development, and also wrote some scattered methods to make it simpler and write less code to complete more verifications. The previous idea was to pass parameters, pass in the ID of the control to be verified, and then pass in the corresponding regular expression if you want to verify the data format. After the project was completed, when I summarized the entire project, I found that this way of writing did not save much code, and in many places, students reported that the library I wrote was not very useful. Although there was an explanation, they also I still can’t understand it very well, I can’t get started quickly, and there should still be a lot of bugs, so in many places they would rather use the method of verifying each control one by one. The verification part of a JS file is just There are two to three hundred lines of code, and they are relatively lazy. They can only write one sentence less for comments, so it is very troublesome to maintain after problems occur. There is no very convenient tool for JS debugging.
I have been on a business trip recently. When I was free, I was thinking whether I could encapsulate more on the basis of the previous one and make it more convenient to call. It is best not to write JS code when calling. I think of jQuery's powerful selectors, and when doing validation or needing to get values from the page, I often added some custom attributes to page elements. Therefore, when you want to perform verification, you only need to add a few custom attributes to the element and call the JS code. This should be the simplest.
This simple validation library should be able to complete 90% of basic validation, including validation when focus is lost and validation when the submit button is clicked. There's nothing I can do about the backend, I can only write it for whoever uses it :).
Let’s start with a piece of calling code. The JS code is too small, so I won’t post it directly. There is an attachment at the end of the article, which also includes a JS file that I wrote myself before for a plug-in that imitates Renren.com. .
For example, verify email:
Sometimes email We allow it to be empty, but once the email is entered, the email is required to be legal. If empty is allowed and the value of empty is assigned to true, then the verification library will not perform non-empty verification on it. If it is false or the empty attribute does not add a default, it means that it is not allowed to be empty. If it is not allowed to be empty, add an emptyErrorMsg attribute to display the error message when it is empty. If this attribute is missing or the value is empty, a red "*" will be displayed. If it is not empty, the value of this attribute will be displayed. . Then there is the verification of the format, which is to verify the email. The value of validata is email. If it is illegal, the value of another custom attribute errorMsg will be displayed. If errorMsg is missing or empty, the error message will also be displayed in red "*".
The value of validata cannot be completely customized, it has been customized in JS. It is to return different regular expressions based on the value of validata. The method is as follows, and the value of the optional validdata is the value of regName of the following method. If the user wants to display it, he can simply add other expressions.
//Return the corresponding regular expression according to different verification content
function returnRegString(regName) {
if (regName == "email") {
return "^([ a-zA-Z0-9_.-]) @(([a-zA-Z0-9-]) .) ([a-zA-Z0-9]{2,4}) $"; //Email
} else if (regName == "tel") {
return "^(86)?(-)?(0[0-9]{2,3})?(-)?([0- 9]{7,8})(-)?([0-9]{3,5})?$"; //Phone
} else if (regName == "phone") {
return "^(13[0-9]|15[0-9]|18[0-9])([0-9]{8})$"; //Mobile phone
} else if (regName == "postcode") {
return "^([0-9]{6})$"; //Postcode
} else if (regName == "number") {
return "^(0 |([1-9] [0-9]*))(.[0-9] )?$"; //Number
} else if (regName == "decimal") {
return " ^[0-9] ([.][0-9] )?$"; //Floating point
} else if (regName == "money") {
return "^([0-9 ])$"; //Currency
} else if (regName == "website") { //Website
return "(http://|https://){0,1}[w/ .?&=] ";
} else if (regName == "fax") { //Fax
return "^[ ]{0,1}([0-9]){1,3} [ ]?([-]?(([0-9])|[ ]){1,12}) $";
} else if (regName == "int") { //Integer
return "^(-){0,1}d $";
} else if (regName == "pInt") { //Positive integer
return "^d $";
} else if (regName == "nInt") { //Negative integers
return "^-d $";
} else if (regName == "nandl") { //Numbers and letters
return "[ a-zA-Z0-9]";
} else if (regName == "chinese") { //Whether it contains Chinese characters
return "[u4e00-u9fa5]";
}
}
No more nonsense, the verification library has not been fully tested yet. If you are interested in trying it out, please let us know if you find any problems or other better improvement methods. Little brother. Although there must be ready-made and relatively mature JS verification libraries, I want to write one myself. Let’s take another screenshot. If the button fails to pass the verification, the pop-up layer will prompt where the verification has not passed. The pop-up layer is the pop-up layer effect that I wrote before to imitate the effect of Renren.

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

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
