Common JS functions

Source: Internet
Author: User

function Getbyclass (oparent,sclass) {//Get element based on class
var oreasult=[];
var oele=oparent.getelementsbytagname ("*");
for (i=0;i<oele.length;i++) {
if (Oele[i].classname==sclass) {
Oreasult.push (Oele[i])
}
};
return oreasult;
}

function GetStyle (obj, name) {//Gets the non-inline style of the element
if (Obj.currentstyle) {
Return obj.currentstyle[name];//ie
}else{
return getComputedStyle (obj, false) [NAME];//FF Chrome
}
}

function css (obj, name, value) {//set or get the style of the element
if (arguments.length==2) {
return obj.style[name];//get
}
else{
obj.style[name]=value;//settings
}
}

function myaddevent (obj, Ev, FN) {//binding event compatibility notation
if (obj.attachevent) {
Obj.attachevent (' On ' +ev, FN); Ie
}else{
Obj.addeventlistener (EV, FN, false);//firefox Chrome
}
}

function removeevent (obj, Ev, FN) {//Remove the compatible wording of the event
if (obj.detachevent) {
Obj.detachevent (' On ' +ev, FN); Ie
}else{
RemoveEventListener (EV, FN, false);//firefox Chrome
}
}

function Stoppropagation (e) {//block event bubbling
E = e | | window.event;
if (window.event) {
E.cancelbubble = true; Ie
} else {
E.stoppropagation ();//firefox
}
}

Prevent Browser from default behavior
function Stopdefault (e) {
Block default browser action (web)
if (e && e.preventdefault)
E.preventdefault ();
How to block the default action of a function in IE
Else
Window.event.returnValue = false;
return false;
}

for (Var i=0;i<oul.childnodes.length;i++) {//Get child nodes
Nodetype==3-> text node
nodetype==1-> element Node
alert (Oul.childnodes[i].nodetype);
if (oul.childnodes[i].nodetype==1) {
oul.childnodes[i].style.background= ' Red ';
}
}


/**
* JS various forms data validation
*/
/**************************************************************************************/
Verification of/************************************* Numbers ***************************************/
/**************************************************************************************/

/**
* Check whether a string of characters is all numbers
* Input: STR string
* Return: TRUE or flase; True is expressed as a number
*/
function Checknum (str) {
Return Str.match (/\d/) = = NULL;
}


/**
* check if a string of characters is a decimal
* Input: STR string
* Return: TRUE or flase; True is expressed as a decimal
*/
function Checkdecimal (str) {
if (Str.match (/^-?\d+ (\.\d+)? $/g) = = null) {
return false;
}
else {
return true;
}
}

/**
* Check whether a string of characters is an integral type of data
* Input: STR string
* Return: TRUE or flase; True is expressed as a decimal
*/
function Checkinteger (str) {
if (Str.match (/^[-+]?\d*$/) = = null) {
return false;
}
else {
return true;
}
}

/**************************************************************************************/
Verification of/************************************* characters ***************************************/
/**************************************************************************************/


/**
* check if a string of input characters is a character
* Input: STR string
* Return: TRUE or flase; True indicates that all characters do not contain Chinese characters
*/
function Checkstr (str) {
if (/[^\x00-\xff]/g.test (str)) {
return false;
}
else {
return true;
}
}


/**
* Check whether the input string contains Chinese characters
* Input: STR string
* Return: TRUE or flase; True indicates that the Chinese character is included
*/
function Checkchinese (str) {
if (Escape (str). IndexOf ("%u")! =-1) {
return true;
}
else {
return false;
}
}


/**
* Check the format of the mailbox you entered is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkemail (str) {
if (Str.match (/[a-za-z0-9_-]+[@] (\s*) (net|com|cn|org|cc|tv|[ 0-9]{1,3}) (\s*)/g) = = null) {
return false;
}
else {
return true;
}
}


/**
* Check the format of the phone number you entered is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkmobilephone (str) {
if (Str.match (/^ (?: 13\d|15[89))-?\d{5} (\d{3}|\*{3}) $/) = = null) {
return false;
}
else {
return true;
}
}


/**
* Check that the fixed phone number entered is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checktelephone (str) {
if (Str.match (/^ ([0\+]\d{2,3}-)? ( 0\d{2,3})-) (\d{7,8}) (-(\d{3,}))? $/) = = null) {
return false;
}
else {
return true;
}
}

/**
* Check whether the format of QQ is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkqq (str) {
if (Str.match (/^\d{5,10}$/) = = null) {
return false;
}
else {
return true;
}
}

/**
* Check that the ID number entered is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkcard (str) {
15-digit ID Regular expression
var arg1 =/^[1-9]\d{7} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3}$/;
18-digit ID regular expression
var arg2 =/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) ((\d{4}) |\d{3}[a-z]) $/;
if (Str.match (arg1) = = null && str.match (arg2) = = null) {
return false;
}
else {
return true;
}
}

/**
* Check that the IP address you entered is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkip (str) {
var arg =/^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $/;
if (Str.match (arg) = = null) {
return false;
}
else {
return true;
}
}

/**
* Check that the URL you entered is the correct address
* Input: STR string
* Return: TRUE or flase; True indicates proper format
*/
function Checkurl (str) {
if (Str.match (/(http[s]?| FTP): \/\/[^\/\.] +?\.. +\w$/i) = = null) {
return False
}
else {
return true;
}
}

/**
* Check if the characters you enter have special characters
* Input: STR string
* Return: TRUE or flase; True indicates that a special character is included
* Used primarily for registration of information when verifying
*/
function Checkquote (str) {
var items = new Array ("~", "'", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
Items.push (":", ";", "'", "|", "\ \", "<", ">", "?", "/", "<<", ">>", "| |", "//");
Items.push ("admin", "Administrators", "Administrator", "Administrator", "system Administrator");
Items.push ("Select", "Delete", "Update", "Insert", "create", "drop", "alter", "Trancate");
str = Str.tolowercase ();
for (var i = 0; i < items.length; i++) {
if (Str.indexof (items[i]) >= 0) {
return true;
}
}
return false;
}


/**************************************************************************************/
Validation of/************************************* time ***************************************/
/**************************************************************************************/

/**
* Check that the date format is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
* Note: Chinese date format cannot be verified here
* Verification Short Date (2007-06-05)
*/
function Checkdate (str) {
var Value=str.match (((1[8-9]\d{2}) | ( [2-9]\d{3})) (-) (10|12|0?[ 13578]) (-) (3[01]|[ 12][0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})) (-) (11|0?) [469]) (-) (30| [12] [0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})) (-) (0?2) (-) (2[0-8]|1[0-9]|0?) [1-9]) $)| (^ ([2468][048]00) (-) (0?2) (-) (29) $) | (^ ([3579][26]00) (-) (0?2) (-) (29) $) | (^ ([1][89][0][48]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][0][48]) (-) (0?2) (-) (29) $) | (^ ([1][89][2468][048]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][2468][048]) (-) (0?2) (-) (29) $) | (^ ([1][89][13579][26]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][13579][26]) (-) (0?2) (-) (29) $)) /);
var value = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2) $/);
if (value = = null) {
return false;
}
else {
var date = new Date (value[1], value[3]-1, value[4]);
Return (date.getfullyear () = = Value[1] && (Date.getmonth () + 1) = = Value[3] && date.getdate () = = Value[4]) ;
}
}

/**
* Check that the time format is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
* Verification Time (10:57:10)
*/
function Checktime (str) {
var value = Str.match (/^ (\d{1,2}) (:)? ( \d{1,2}) \2 (\d{1,2}) $/)
if (value = = null) {
return false;
}
else {
if (value[1] > | | value[3] > | | value[4] > 60) {
return False
}
else {
return true;
}
}
}

/**
* Check that the full date time format is correct
* Input: STR string
* Return: TRUE or flase; True indicates proper format
* (2007-06-05-10:57:10)
*/
function Checkfulltime (str) {
var value = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2});
var value = Str.match (/^ (?: 19|20) [0-9][0-9]-(?:(?: 0 [1-9]) | (?: 1[0-2])) -(?:(?: [0-2][1-9]) | (?: [1-3][0-1])) (?:(?: [0-2][0-3]) | (?: [0-1][0-9]): [0-5][0-9]:[0-5][0-9]$/);
if (value = = null) {
return false;
}
else {
var date = new Date (checkfulltime[1], checkfulltime[3]-1, checkfulltime[4], checkfulltime[5], checkfulltime[6], check FULLTIME[7]);
Return (date.getfullyear () = = Value[1] && (Date.getmonth () + 1) = = Value[3] && date.getdate () = = Value[4 ] && date.gethours () = = Value[5] && date.getminutes () = = Value[6] && date.getseconds () = = Value[7] );
return true;
}

}


/**************************************************************************************/
Verification of/************************************ ID number **********************************/
/**************************************************************************************/

/**
* ID Card 15-bit encoding rules: DDDDDD YYMMDD xx p
* DDDDDD: Area code
* YYMMDD: Birth date
* XX: Sequential class encoding, unable to determine
* P: Gender, odd male, even female
* <p/>
* ID card 18-bit encoding rule: dddddd yyyymmdd xxx y
* DDDDDD: Area code
* YYYYMMDD: Birth date
* XXX: Sequential class encoding, unable to determine, odd for male, even for female
* Y: Check code, this bit value can be obtained by the first 17 bit calculation
* <p/>
* 18-digit weighted factor (right-to-left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1]
* Verify Bit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]
* Check digit calculation formula: y_p = mod (∑ (AIXWI), 11)
* I for the ID card number from right to left number of 2...18 bit; Y_p is the location of the check code array where the feet check code
*
*/
var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];//weighting factor
var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];//ID verify the bit value. 10 for X
function Idcardvalidate (idcard) {
Idcard = Trim (idcard.replace (//g, ""));
if (idcard.length = = 15) {
Return Isvaliditybrithby15idcard (Idcard);
}
Else
if (idcard.length = = 18) {
var a_idcard = Idcard.split ("");//Get ID Array
if (Isvaliditybrithby18idcard (Idcard) && Istruevalidatecodeby18idcard (A_idcard)) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}

/**
* Determines if the last verification bit is correct when the ID number is 18 bits
* @param a_idcard ID number Array
* @return
*/
Function isTrueValidateCodeBy1 8IdCard (a_idcard) {
var sum = 0;//Declare weighted sum variable
if (a_idcard[17].tolowercase () = = ' x ') {
A_idcard[17] = 10;//will last The verification code bit x is replaced with 10 to facilitate subsequent operation
}
for (var i = 0; i < i++) {
sum + = wi[i] * a_idcard[i];//weighted sum
}
VALCODEP osition = sum% 11;//Get the verification code location
if (a_idcard[17] = = Validecode[valcodeposition]) {
return true;
}
Else {
return false;
}
}

/**
* ID is male, female,
* @param idcard 15/18-digit ID number
* @return ' female '-female, ' male '-male
*/
Function Maleorfemal Byidcard (idcard) {
Idcard = Trim (idcard.replace (//g, ""));//Handle the ID card number. Include spaces between characters.
if (idcard.length = =) {
if (idcard.substring (2)% = = 0) {
return ' female ';
}
Else {
return ' male ';
}
}
Else
if (idcard.length = =) {
if (idcard.substring (+),% 2 = = 0) {
return ' female ';
}
Else {
return ' male ';
}
}
Else {
return null;
}
}

/**
* Verify that birthdays in 18-digit ID numbers are valid birthdays
* @param idcard 18-bit book ID string
* @return
*/
Function isvaliditybrithby18i Dcard (idCard18) {
var year = idcard18.substring (6, ten);
var month = idcard18.substring (ten);
var day = Idcar D18.substring (12, 14);
var temp_date = new Date (year, parsefloat (month) – 1, parsefloat (day));
//Here Use getFullYear () to get the year, avoid the millennium bug problem
if (temp_date.getfullyear ()! = parsefloat (year) | |
Temp_date.getmonth ()! = parsefloat (month)-1 | |
Temp_date.getdate ()! = parsefloat (day)) {
return false;
}
Else {
return true;
}
}

/**
* Verify that birthdays in 15-digit ID numbers are valid birthdays
* @param idCard15 15-bit book ID string
* @return
*/
function Isvaliditybrithby15idcard (IDCARD15) {
var year = idcard15.substring (6, 8);
var month = idcard15.substring (8, 10);
var day = idcard15.substring (10, 12);
var temp_date = new Date (year, parsefloat (month) – 1, parsefloat (day));
Use the GetYear () method for the age of your old ID card without considering the millennium bug problem
if (Temp_date.getyear ()! = parsefloat (year) | |
Temp_date.getmonth ()! = parsefloat (month)-1 | |
Temp_date.getdate ()! = parsefloat (day)) {
return false;
}
else {
return true;
}
}

Remove string kinsoku and trailing spaces
function Trim (str) {
Return Str.replace (/(^\s*) | ( \s*$)/g, "");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.