This article summarizes the various operations common to JavaScript, including string, time, form, regular validation, and so on. Has a very high reference value. Share for everyone to use for reference. The specific method is as follows:
/***** basepage.js Common script file part methods need to refer to jquery library *****///#region Date operation//String conversion to time.
function Stringtotime (date1) {var dt = new Date (Date.parse (Date1.replace (/-/g, "/"));
return DT;
///use var date1 = "2013-06-08 15:23:31" or "2013/6/8 9:9:00" format;
-------------------------------------------------------------//date format Date.prototype.format = function (format) {
var o = {"m+": This.getmonth () + 1,//month "d+": This.getdate (),//day "h+": this.gethours (),//hour "m+": This.getminutes (),//minute "s+": This.getseconds (),//second "q+": Math.floor ((This.getmonth () + 3)/3), Quarter "S": This.getmilliseconds ()//millisecond} if (/(y+)/.test (format)) {format = Format.replace (
Regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) {if (New RegExp ("+ K +")). Test (format) {format = Format.replace (regexp.$1, REGEXP . $1.length = 1?
O[k]: ("+ o[k]"). substr (("" "+ o[k]). length); }
} return format;
////use Method//alert (Newdate.format ("Yyyy-mm-dd"));
Alert (Newdate.format ("mm/dd/yyyy")); var nowstr = Now.format ("Yyyy-mm-dd hh:mm:ss");
Math.Round (Math.random () * 10000)////use Method 2://var testdate = new Date ();
var teststr = Testdate.format ("yyyy mm month DD Day hh hour mm minute ss seconds");
Example://alert (Testdate.format ("yyyy mm month DD day")); -------------------------------------------------------------//Set Date (array) function Setflag (start, end) {var
CDate = Array ();
CDate = Start.split ("-");
var cd = cdate[1] + "/" + cdate[2] + "/" + cdate[0];
var daynum = DateDiff (end, start);
for (var i = 0; I <= daynum i++) {Flag.push (AddDays (CD, i));
}//end The new date after the fun//date plus the number of days.
function AddDays (date, days) {var nd = new Date (date);
nd = nd.valueof ();
nd = nd + days * 24 * 60 * 60 * 1000;
nd = new Date (ND);
Alert (nd.getfullyear () + "year" + (Nd.getmonth () + 1) + "month" + nd.getdate () + "Day");
var y = nd.getfullyear ();
var m = nd.getmonth () + 1; var d = nd. GetDate ();
if (M <= 9) m = "0" + m;
if (d <= 9) d = "0" + D;
var CDate = y + "-" + M + "-" + D;
return cdate;
}//Two-date difference (D1-D2).
function DateDiff (d1, D2) {var day = 24 * 60 * 60 * 1000;
try {var Datearr = D1.split ("-");
var checkdate = new Date ();
Checkdate.setfullyear (Datearr[0], datearr[1]-1, datearr[2]);
var checktime = Checkdate.gettime ();
var dateArr2 = D2.split ("-");
var checkDate2 = new Date ();
Checkdate2.setfullyear (Datearr2[0], datearr2[1]-1, datearr2[2]);
var checkTime2 = Checkdate2.gettime ();
var cha = (checktime-checktime2)/day;
return cha;
catch (e) {return false; }//end Fun//#endregion//#region URL operation/* * Obtain value based on querystring parameter name */function Getquerystringbyname (name) {var
result = Location.search.match (new RegExp ("[\?\&]" + name + "= ([^\&]+)", "I"));
if (result = = NULL | | Result.length < 1) return "";
return result[1]; } function getquerystring (name){///If the link has no parameters, or the link does not exist The parameter we want to get, return the null if (Location.href.indexOf ("?") = = 1 | | location.href.indexOf (name + ' = ') = = 1)
{return ';
//Get the parameter portion of the link var querystring = location.href.substring (Location.href.indexOf ("?") + 1);
Separation parameters for? key=value&key2=value2 var parameters = Querystring.split ("&");
var pos, paraname, Paravalue;
for (var i = 0; i < parameters.length i++) {//get equal position pos = parameters[i].indexof (' = ');
if (pos = = 1) {continue;
}//Get name and value Paraname = parameters[i].substring (0, POS);
Paravalue = parameters[i].substring (pos + 1); If the name of the query is equal to the current name, the current value is returned, and the + number in the link is restored to a space if (paraname = = name) {return unescape (paravalue.replace/\+/g, ""
));
} return ';
* * * Get the lowercase filename of the current address/function Getcurrenturlname () {var url = window.location.href;
url = url.tolocalelowercase ();
if (url = = Undefined | | url = "") return "";
var item = url.tostring (). Split ("/"); Var name = item[item.length-1];
Name = Name.tostring (). Split (".");
if (name.length = = 2) return name[0].tostring ();
else return "";} #endregion//#region string operation/* Intercept specified length string * parameter: * strstring: * Strstart: Start index * Intlen: Length of interception * FU Nction getcustomlengtstr (strstring, Strstart, Intlen) {if (strstring!= undefined && strstring!= "" &&
strstring!= null) {var CLen = strstring.tostring (). length;
if ((Strstart + intlen) <= CLen-1) {if (Strstart < 0) Strstart = 0;
Return strstring.tostring (). substr (Strstart, Number (Intlen));
else {//length out of bounds, return original data returned strstring;
} else return "";} * * Intercepts the String * parameters for the specified interval: * strstring: * Intstart: The string to be intercepted * intend: The index of the beginning * is the getcustomlengtstr of the end of the index * (strst Ring, Intstart, intend) {if (strstring!= undefined && strstring!= "" && strstring!= null) {var
Clen = Strstring.tostring (). length-1; if (Number (Intend) <= Clen) {if (Intstart < 0) Intstart = 0;
else if (Intstart > Clen) intstart = Clen;
Return strstring.tostring (). substring (intstart, intend);
else {//length out of bounds, return original data returned strstring;
} else return "";} #endregion//#region homepage, add favorites//Set homepage function sethome () {if (document.all) {Document.body.style.behavior =
"URL (#default #homepage)";
var url = window.location.href;
Document.body.setHomePage (URL); else {alert ("Set as homepage failed, please set manually!")
");
}//Add to Collection function Addcollect () {var url = window.location.href;
try {window.external.addFavorite (URL, "Mei Yuan Gold Industry");
catch (e) {try {window.sidebar.addPanel ("Mei Yuan Gold", url, "");
catch (E) {alert ("Join collection failed, please use Ctrl+d to add");
}}//#endregion//#region Select All/All//checkselectall (TRUE);
function Checkselectall (check) {$ ("input[type= ' checkbox ']"). attr ("Checked", check)}//#endregion//#region Verification Code Countdown Countdown ("#btnGetCode ", 60);
function countdown (item, times) {//The element to be manipulated, time (s) var timer = setinterval (function () {var btnvalidate = $ (item);
if (Times > 0) {btnvalidate.attr ("disabled", "false"). CSS ("opacity", "0.5"). Val ("Re-get (+ times +)");
times--;
else {btnvalidate.removeattr ("disabled"). CSS ("opacity", "1"). Val ("Get the Captcha");
Clearinterval (timer);
}, 1000);
}//#endregion//#region Clear the default value of the text box//clearempty ("#txtName"); function Clearempty (obj) {//element $ (obj) to be manipulated. Focus (function () {if ($ (this). val () = This.defaultvalue) {$ (this
). Val ("");
}). blur (function () {if ($ (this). val () = "") {$ (this). Val (This.defaultvalue);
}
});
//#endregion//#region refreshing page function refresh () {window.parent.location.reload ();}
#endregion//#region Form validation//#region Check for Chinese//var item = Checkchinese ("Chinese");
function Checkchinese (obj) {var reg =/[^\u4e00-\u9fa5]/;
return!reg.test (obj); }//#endregion
#region Check if the number//var item = Checknum ("123");
function Checknum (obj) {var reg =/^\d+$/;
return reg.test (obj);
}//#endregion//#region Check for the letter//var item = Checkletter ("abc");
function Checkletter (obj) {var reg =/^[a-za-z]+$/;
return reg.test (obj);
//#endregion//#region Check if the letter or number//var item = checkletternum ("abc123");
function Checkletternum (obj) {var reg =/^[a-za-z0-9]+$/;
return reg.test (obj);
//#endregion//#region Check if the letter or Chinese//var item = checkletternum ("abc123");
function Checkletterchina (obj) {var reg =/^[a-za-z\u4e00-\u9fa5]+$/;
return reg.test (obj);
//#endregion//#region Check if the letter or Chinese or digital//var item = checkletternum ("abc123");
function Checkletterchinanum (obj) {var reg =/^[a-za-z0-9\u4e00-\u9fa5]+$/;
return reg.test (obj); //#endregion//#region Check mobile phone number format//var item = checkmobile ("13888888888") function Checkmobile (obj) {var reg =
/^[1][3458][0-9]{9}$/;
return reg.test (obj); }//#endRegion//#region Check mailbox Format//var item = checkemail ("abc@123.com") function Checkemail (obj) {var reg =/^ ([a-za-z0-9 _\.\-]) +\@ (([a-za-z0-9\-]) +\.)
+ ([a-za-z0-9]{2,4}) +$/;
return reg.test (obj); }//#endregion//#region Check ID card format//var item = Checkidcard ("555555555555555555") function Checkidcard (obj) {var Reg =/(^\d{15}$) | (^\d{18}$) | (^\d{17} (\d|
X|X) $)/;
return reg.test (obj);
}//#endregion///random number function Getrandomnum (Min, Max) {var Range = max-min;
var Rand = Math.random ();
Return (Min + math.round (Rand * Range));
}//#endregion
I believe that this article describes the use of JavaScript in the web design has a good reference value.