Home > Web Front-end > JS Tutorial > body text

js common functions good_basic knowledge

WBOY
Release: 2016-05-16 19:26:15
Original
876 people have browsed it

1. Regular functions
JavaScript regular functions include the following 9 functions:
(1) alert function: displays a warning dialog box, including an OK button.
 (2)confirm function: Display a confirmation dialog box, including OK and Cancel buttons.
 (3)escape function: Convert characters into Unicode codes.
(4) eval function: calculates the result of an expression.
(5) isNaN function: Test whether (true) or not (false) is not a number.
 (6)parseFloat function: Convert a string into dotted digital form.
 (7)parseInt function: Convert the string into integer digital form (the decimal system can be specified).
 (8)prompt function: Display an input dialog box, prompting to wait for user input. For example:
 (9)unescape function: Decode characters encoded by the escape function.
2. Array function
JavaScript array function includes the following 4 functions:
(1) join function: convert and connect all elements in the array into a string. Example:

Program code
Function JoinDemo()
{
var a, b;
a = new Array(0,1,2,3,4);
b = a.join("-");//Separator
return(b);//Returned b=="0-1-2-3-4"
}  

(2)langth function: Returns the length of the array. Example:
 
Program code
function LengthDemo()
 {
 var a, l;
 a = new Array(0,1,2,3,4);
  l = a.length;
  return(l);//l==5
  } 

 (3)reverse function: Reverse the order of array elements. Example:
 
Program code
function ReverseDemo()
 {
 var a, l;
 a = new Array(0,1,2,3,4);
l = a.reverse();
return(l);
}
(4) Sort function: Reorder array elements. Example:
 
Program code
function SortDemo()
 {
 var a, l;
a = new Array("X","y","d", Z", "v","m","r");
l = a.sort();
return(l);
}
3. Date function
javascript The date function includes the following 20 functions:
(1) getDate function: returns the "day" part of the date, with values ​​from 1 to 31. Example:
 
Program code
function DateDemo()
 {
 var d, s = "Today's date is: ";
 d = new Date();
 s = (d.getMonth() 1) "/";
s = d.getDate() "/";
s = d.getYear();
return(s);
}
(2) getDay function: Returns the day of the week, the value is 0~6, where 0 means Sunday, 1 means Monday,..., 6 means Saturday. Example:
 
Program code
function DateDemo()
 {
 var d, day, x, s = "Today is: ";
 var x = new Array("Sunday ", "Monday", "Tuesday");
var x = x.concat("Wednesday", "Thursday", "Friday");
var x = x.concat("Saturday");
d = new Date();
day = d.getDay();
return(s = x[day]); hour" part, the value is 0~23. example.
 
Program code
function TimeDemo()
 {
 var d, s = "The current local time is: ";
var c = ":";
 d = new Date();
 s = d.getHours() c;
s = d.getMinutes() c;
s = d.getSeconds() c;
s = d.getMilliseconds ();
Return(s);
See the example above.
(5) getMonth function: Returns the "month" part of the date, with a value of 0 to 11. Among them, 0 represents January, 2 represents March, ..., and 11 represents December. See the previous example
.
(6) getSeconds function: Returns the "seconds" part of the date, the value is 0~59. See previous example.
 (7)getTime function: Returns the system time.
 
Program code
function GetTimeTest()
 {
 var d, s, t;
 var MinMilli = 1000 * 60; 🎜> var DyMilli = HrMilli * 24;
d = new Date();
t = d.getTime();
s = "It's been "
s = Math.round(t / DyMilli) " days since 1/1/70";
return(s);
time difference) in minutes.
 
Program code
function TZDemo()
 {
 var d, tz, s = "The current local time is ";
d = new Date();
tz = d.getTimezoneOffset();

if (the name of the link point of tz, the URL address set by another function link.
(2) big function: increase the font to size one, and ...The label result is the same. (3) Blink function: Makes the string blink, which is the same as the ... label result.
(4) Bold function: Makes the font bold, which is the same as the ... label result. .
(5) charAt function: Returns a character specified in the string. (6) Fixed function: Sets the font to a fixed-width font, the same as... tag result. 7) Fontcolor function: Set the font color, which is the same as the label result. (8) Fontsize function: Set the font size, which is the same as the label result. (9) IndexOf function: Return the first one in the string. Find the subscript index, starting from the left.
 (10)italics function: Make the font italic, the same as... tag result.
 (11)lastIndexOf function: return the first one in the string. Find the subscript index, starting from the right.
 (12)length function: Return the length of the string (without parentheses)
 (13)Link function: Generate a hyperlink, equivalent to setting.
 (14) Small function: Reduce the font size by one size, the same result as... tag.
 (15) Strike function: Add a horizontal line in the middle of the text, as... .The label result is the same.
 (16) Sub function: Display the string as a subscript (17) Substring function: Return the specified characters in the string. sup function: Display the string as superscript. (19) toLowerCase function: Convert the string to lowercase. (20) ToUpperCase function: Convert the string to uppercase.

//Check whether it is all composed of numbers

Program code
function isDigit(s)
{
var patrn=/^[0-9]{1 ,20}$/;
if (!patrn.exec(s)) return false
return true
}


//Verify login name: only 5 can be entered -20 strings starting with letters and can contain numbers, "_", "."

Program code
function isRegisterUserName(s)
{
var patrn=/^[ a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}


//Verify user name: Only 1-30 strings starting with letters can be entered

Program code
function isTrueName( s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}

//Verification password: only 6-20 letters, numbers, and underscores can be entered

Program code
function isPasswd(s)
{
var patrn=/^(w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}


/ /Verify ordinary telephone and fax numbers: It can start with " ", and in addition to numbers, it can contain "-"

Program code
function isTel(s)
{
//var patrn =/^[ ]{0,1}(d){1,3}[ ]?([-]?(d){1,12}) $/;
var patrn=/^[ ]{0 ,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12}) $/;
if (!patrn.exec(s)) return false
return true
}


//Verify mobile phone number: must start with a number. In addition to numbers, it can contain "-"

Program code
function isMobil(s)
{
var patrn=/^[ ]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){ 1,12}) $/;
if (!patrn.exec(s)) return false
return true
}

//Verify postal code

Program code
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[ a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}

//Verification Search keywords

Program code
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*() =|\][ ]{}:;',./?]{1}[^`~!@$%^&() =|\][]{}:;',.?]{0, 19}$/;
if (!patrn.exec(s)) return false
return true
}


Program code
function isIP(s) // by zergling
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!