In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly describes how to use JavaScript and regular expressions for data validation, the article is very detailed, has a certain reference value, interested friends must read!
Data validation is an important step for web applications to accept data from clients; after all, you need to ensure that customer data is in the expected format before using it. In web applications, you can choose to use platform-specific tools, such as ASP.NET, JSP, and so on, or you can take advantage of client-side JavaScript, where regular expressions simplify data validation.
regular expression
Regular expressions are pattern-matching tools that allow you to express patterns in literal terms, making them a powerful tool for validating textual data. In addition to pattern matching, regular expressions can also be used for literal substitution. Support for regular expressions has continued to expand since I first encountered them when I was working with Perl on UNIX systems.
Note: If you are surrounded by many other developers, regular expressions may be called RegEx or RegExp. Although regular expressions are powerful, their syntax is a bit "arcane" and takes some time to master, so let's look at some of the basics of using regular expressions.
basic syntax
The syntax of regular expressions can be complex enough to require an entire book on the topic, but I'll cover some of the basics to help you get a feel for regular expressions.
A basic concept is an anchor, which allows you to specify the start and end of a string, with the caret (^) used to specify the start of the string and the dollar sign ($) used to indicate the end. If you need to include a dash or dollar sign in your query string, you can use escape sequences to do so. The escape character () takes precedence over the dash or dollar sign. The following example matches when the word search appears in a string.
^search$
Also, you can find a set of characters by simply placing them in square brackets, such as [ and ], to which the matching characters must belong. An example would be finding matching numbers 1 through 5 in the range [12345], or the regular expression could be written as [1-5].
Many times you may need to specify characters that can occur multiple times, or optional characters, question marks (?) A plus sign (+) means that the character can occur once or more times, and an asterisk (*) means that the character can occur none or more times.
Now let's see how to apply these simple regular expressions to JavaScript.
JavaScript support
JavaScript added support for regular expressions in version 1.2, browser support began with Internet Explorer 4 and Netscape 4, and all Firefox versions and most modern browsers include JavaScript support. Regular expressions can be used with JavaScript strings and RegExp.
use string
Every JavaScript string can support regular expressions in three ways: match(), replace(), and search(), and the object's test() method also allows you to test. Here is information about the match(), replace(), and search() methods:
match(): Used for regular expression matching, if multiple matches occur, returns an array containing all the matching results, each entry in the array is a copy containing the matching data; if there is no match, returns null.
replace(): Used to match regular expressions and replace all matching values with new substrings. The first parameter of this method is the regular expression, and the second parameter is the string to be replaced.
search(): Used to search for a match between a regular expression and a specified string, returning the index value of the string if a match occurs, or-1 if no match occurs.
JavaScript also provides RegExp objects to create and use regular expressions.
RegExp
RegExp objects contain patterns of regular expressions whose methods and properties can be used to match strings. There are two ways to create instances of RegExp objects: using constructors or literal patterns of regular expressions. The second parameter is optional and specifies that the search is global (g), case-insensitive (i), or global and case-insensitive (gi). The following example is how to create a RegExp object using a constructor, in which case of the search object is ignored:
testRegExp = new RegExp("^search$","I")
You can create the same instance (part in slash) using literal means, as follows:
testRegExp = /^search$/i
The RegExp object contains a large number of methods, but we'll cover only one of them, test. This method will perform regular expression matching on the specified string, returning true if successful and false if failed. This method can be applied to literal strings or string variables. Basically, it allows you to perform regular expression matching on a string. The following example demonstrates how to use this method:
testRegExp = /search/i;if (testRegExp.test("this is a search string") {[xss_clean]("The string was found. ");} else {[xss_clean]("No match found. ");}We can place it in a Web page to test:RegExp testtestRegExp = /search/i;if (testRegExp.test("this is a search string")) {alert("The string was found. ");} else {alert("No match found. ");}
actual operation
Now it's time for a more complete example. The page in Listing A contains JavaScript methods to validate the values entered in the text box. This JavaScript code searches for strings containing my last name and my first name (ignoring case), and if my first name is found, replaces it with a short name by searching for string objects. The second text box is for accepting time values, where a regular expression validates the time entered (numbers separated by colons). This simple example shows how to incorporate regular expressions into your client code for matching and replacing:
RegExp validationfunction validate() {var doc = document.test;varvalName = new RegExp("^(Tony|Anthony) Patton", "i");if (doc.Name.value.match(valName) == null) {alert("Name was not found. ");} else {doc.Name.value = doc.Name.value.replace(valName, "T. Patton");}varvalTime = new RegExp("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$");if (doc.time.value.match(valTime) == null) {alert("Please enter correct time format (hh:ss)");} }Name:
Time:
powerful and complex
Regular expressions are powerful, but they're not easy to use, so you should learn them step by step, but it's certainly worth the time to learn how to use them correctly. Regular expressions provide a simple and elegant way to manipulate text for JavaScript (and other languages) and generic software for form validation.
The above is "How to use JavaScript and regular expressions for data validation" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.