js common functions_ 01 about the use of model(), substr() and regular expressions
1,model()
Bootstrap Modal plug-in
A Modal box is a child form that is overlaid on the parent form. Typically, the purpose is to display content from a separate source, with some interaction without leaving the parent form. Subforms can provide information, interaction, etc.
If you want to reference the functions of the plug-in separately, you need to reference modal.js.
detailed description https://www.runoob.com/bootstrap/bootstrap-modal-plugin.html
2,substr()
example
// Extract the specified number of characters: var str="Hello world!"; var n=str.substr(2,3) // n output results: // llo // Use substr() to extract some characters from the second position of the string: var str="Hello world!"; var n=str.substr(2) // n output results: // llo world!
Definition and Usage
The substr() method extracts a specified number of characters from the string starting with the starting subscript.
Tip: the substr() parameter specifies the start position and length of the substring, so it can be used instead of substring() and slice().
In IE 4, the value of the parameter start is invalid. In this BUG, start specifies the position of the 0th character. This BUG has been fixed in later versions.
ECMAscript does not standardize this method, so it is opposed to its use.
Note: the substr() method does not change the source string.
grammar
string.substr(start,length)
Parameter value
parameter | describe |
---|---|
start | Required. The starting subscript of the substring to extract. Must be numeric. If it is negative, the parameter declares the position from the end of the string. That is, - 1 refers to the last character in the string, - 2 refers to the penultimate character, and so on. |
length | Optional. The number of characters in the substring. Must be numeric. If this parameter is omitted, the string from the beginning to the end of the stringObject is returned. |
Return value
type | describe |
---|---|
String | A new string containing the extracted part of the text |
3. Use of regular expressions
example
var txtvalve = this.value; var reg = new RegExp(/^\d+$/); if(!reg.test(txtvalve)){ alert("Number must be entered") }
The test() method is used to detect whether a string matches a pattern
grammar
RegExpObject.test(string)
parameter | describe |
---|---|
string | Required. String to detect. |
Return value
If the string string contains text matching RegExpObject, it returns true; otherwise, it returns false.
(source: https://www.w3school.com.cn/jsref/jsref_test_regexp.asp)
explain
Call the test() method of RegExp object R and pass it the string s, which is equivalent to this expression: (r.exec (s)! = null).
The exec() method is used to retrieve a match for a regular expression in a string.
exec() method syntax
RegExpObject.exec(string)
parameter | describe |
---|---|
string | Required. The string to retrieve. |
Return value
Returns an array containing matching results. If no match is found, the return value is null.
explain
The exec() method is very powerful. It is a general method and is more complex to use than the test() method and the String object that supports regular expressions.
If exec() finds a matching text, it returns a result array. Otherwise, it returns null. The 0 element of this array is the text matching the regular expression, the 1 element is the text matching the first sub expression of RegExpObject (if any), and the 2 element is the text matching the second sub expression of RegExpObject (if any), and so on. In addition to the array element and the length attribute, the exec() method returns two attributes. The index attribute declares the position of the first character of the matching text. The input attribute stores the retrieved string string. We can see that when calling exec() of a non global RegExp object Method, the returned array is the same as the array returned by calling the method String.match().
However, when RegExpObject is a global regular expression, the behavior of exec() is slightly more complex. It will start to retrieve the string at the character specified by the lastIndex property of RegExpObject When the text matching the expression is found, after matching, it will set the lastIndex property of RegExpObject to the next position of the last character of the matching text. That is, you can iterate through all the matching text in the string by calling the exec() method repeatedly. When exec()) When no matching text is found, it returns null and resets the lastIndex property to 0.
Tips and notes
Important: if you want to start retrieving a new string after completing a pattern match in a string, you must manually reset the lastIndex property to 0.
Tip: Please note that whether RegExpObject is in global mode or not, exec() will add the full details to the array it returns. This is the difference between exec() and String.match(), which returns much less information in global mode. Therefore, we can say that exec() is called repeatedly in the loop Method is the only method to obtain the complete pattern matching information of the global pattern. (source: https://www.w3school.com.cn/jsref/jsref_exec_regexp.asp)
example
// In this example, we will globally retrieve W3School in the string: <script type="text/javascript"> var str = "Visit W3School"; var patt = new RegExp("W3School","g"); var result; while ((result = patt.exec(str)) != null) { document.write(result); document.write("<br />"); document.write(patt.lastIndex); } </script> // Output: W3School 14
Some common examples of input checking through regular expressions
$.validator.methods.engnum = function(value, element) { // Half width alphanumeric var reg = /^[0-9a-zA-Z]+$/; return this.optional(element) || reg.test(value); }; $.validator.methods.engnumhyphen = function(value, element) { // Half width alphanumeric, - (hyphen) var reg = /^[0-9a-zA-Z\-]+$/; return this.optional(element) || reg.test(value); }; $.validator.methods.engnumhyphenspace = function(value, element) { // Half width alphanumeric, - (hyphen), half width space var reg = /^[0-9a-zA-Z\- ]+$/; return this.optional(element) || reg.test(value); }; $.validator.methods.halfnum = function(value, element) { // Half angle digit var reg = /^[0-9]*$/; return this.optional(element) || reg.test(value); }; $.validator.methods.allchar = function(value, element) { var reg = /^[\x20-\x7e]+$/; return this.optional(element) || reg.test(value); }; $.validator.methods.date1 = function(value, element) { // date pattern: ddMMMyy hhmm var reg = /^(\d{2})(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\d{2}[ ]{1}\d{2}\d{2})$/gi; return this.optional(element) || validateDate(value, element, reg); }; $.validator.methods.date2 = function(value, element) { // date pattern: DDMMMYY OR DDMMMYYYY var reg = /^(\d{2})(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\d{2}|\d{4})$/gi; return this.optional(element) || validateDate(value, element, reg); }; $.validator.methods.date3 = function(value, element) { // date pattern: DDMMMYY OR DDMMMYYYY var reg = /^(\d{2})(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\d{2})$/gi; return this.optional(element) || validateDate(value, element, reg); };
4,this.optional
this.optional(element) is a function in the jquery.validator.js form validation framework. It is used to trigger validation only when the value of the form control is not empty.
Simply put, when the value of the form control is empty, the form verification will not be performed. This function will return true to indicate that the verification has passed. When the value of the form control is not empty, the subsequent verification condition judgment will be executed and the verification result will be returned as true or false. Generally, when returning the return value of this function, you will use | to connect a verification condition later.
The example code is as follows:
jQuery.validator.addMethod("number", function(value, element) { return this.optional(element) || /[0-9]{1,2}/.test(value); }, "Please enter 0-99 Integer of");
In this example, when the value of the form control to be verified is null, it will directly return true, that is, the form has passed verification. When the value of the form control to be verified is not null, the regular expression following | will be executed to match an integer of 0-99. If the matching is successful, it will return true, otherwise it will return false.