Common javascriptfunction code _ javascript skills

Source: Internet
Author: User
Some common javascript Functions are used as reference for learning. // mhHover ('start ID name', 'nth TD tag', 'style name ')

Program code

Function mhHover (tbl, idx, cls ){
Var t, d;
If (document. getElementById) t = document. getElementById (tbl );
Else t = document. all (tbl );
If (t = null) return;
If (t. getElementsByTagName) d = t. getElementsByTagName ("TD ");
Else d = t. all. tags ("TD ");
If (d = null) return;
If (d. length <= idx)
Return;
D [idx]. className = cls;
}

// Drop-down menu to show hidden IDS
// Usage
// OnChange = "OptionNumber_change ('form name', 'Id with the same name (sequence number automatically taken )')"
// Note: the Value of the form must be a number.

Program code

Function OptionNumber_change (selObj, strOpt)
{
Var a = (selObj. options [selObj. selectedIndex]. value );
For (I = 1; I <= a; I ++)
{
Document. getElementById (strOpt + I). style. display = "";
}
For (j = 10; j> a; j --)
{
Document. getElementById (strOpt + j). style. display = "none ";
}
}

// Read the database and show the hidden ID
// Usage
//

Program code

Function ListNumber_change (ListObj, ListStr)
{
Var a = ListObj;
For (I = 1; I <= a; I ++)
{
Document. getElementById (ListStr + I). style. display = "";
}
For (j = 10; j> a; j --)
{
Document. getElementById (ListStr + j). style. display = "none ";
}
}

// Iframe adaptive height
// Usage:
// OnLoad = "reSizeFrame ('iframe ID name ')"

Program code

Function reSizeFrame (frameID) {// frameID is your ifream name
Try {
Var oBody = eval (frameID + ". document. body ");
Var oFrame = document. all (frameID );
OFrame. style. height = oBody. scrollHeight + (oBody. offsetHeight-oBody. clientHeight );
}
Catch (e ){
Window. status = 'error: '+ e. number +'; '+ e. description;
}
}

// Select all check boxes
// Usage:
// Onclick = CheckAll (this. form, this. checked)

Program code

Function CheckAll (form1, v ){
For (var I = 0; I Var e = form1.elements [I];
If (e. type = 'checkbox') e. checked = v;
}
}

// Verify the correctness of the form string expression

Program code

Function regExpCheck (re, getO, msg)
{
If (re. test (getO. value) = false)
{
If (getO. msg)
{
Msg = getO. msg + msg
}
Alert (msg)
GetO. focus ();
Return false;
}
Return true;
}

// Verify the user name

Program code

Function checkAccount (getO)
{
Re =/^ ([a-z0-9] {}) $/gi
Msg = "username format: 4-12 digits in lowercase (a-z) (0-9 )"
Return regExpCheck (re, getO, msg)
}

// Verify the password

Program code

Function checkPassY (getO)
{
Re =/^ ([A-Za-z0-9] {8, 12}) $/gi
Msg = "Login Password format: 8-12 English (A-Z, a-z) number (0-9 )"
Return regExpCheck (re, getO, msg)
}

// Verify the real name

Program code

Function checkNameN (getO)
{
Re =/^ [^ \ '\, \ ;\\ |\@\*] {} $/gi
Msg = "Incorrect real name format"
Xx = regExpCheck (re, getO, msg)
If (xx = true)
{
Re =/[^-}]/gi
GV = getO. value
If (gV. match (re) = null)
Return true
Else
{
GV_c = (gV. match (re). length
GV_e = V. length-gV_c
Gv_len = gV_c * 2 + gV_e
If (gv_len> 20)
{
Alert ("cannot exceed 10 characters ")
Return false;
}
Else
Return true
}
}
Else
Return false;
}

// Verify the ID card number

Program code

Function checkIdCard (getO)
{
Re =/^ ([0-9] {15}) | ([0-9a-z] {18}) $/gi;
Msg = "Incorrect ID card format"
Return regExpCheck (re, getO, msg)
}

// Verify the email

Program code

Function checkEmail (getO)
{
Re =/^ ([\. \ w-]) {2, 30} @ ([\ w-]) {3, 30 }(\. ([\ w]) {2, 4}) {1, 2}/gi
Msg = "Incorrect email format"
Return regExpCheck (re, getO, msg)
}

// Verify the zip code

Program code

Function checkZipCode (getO)
{
Re =/^ ([0-9] {6}) $/gi
Msg = "zip code format: 6 digits (0-9 )"
Return regExpCheck (re, getO, msg)
}

// Verify the phone number

Program code

Function checkPhone (getO)
{
Re =/^ ([\ d]) {2, 5}-([\ d]) {3, 14}/gi
Msg = "phone format error \ r \ n region number and phone number should be separated by"-", for example: 029-82303721"
Return regExpCheck (re, getO, msg)
}

// Verify the mobile phone number

Program code

Function checkMobile (getO)
{
Re =/^ (\ +? [0-9] {0, 3} 13 [0-9] {9}) $/gi
Msg = "Incorrect mobile phone format"
Return regExpCheck (re, getO, msg)
}

// Verify the verification code

Program code

Function checkVcode (getO)
{
Re =/^ (\ d {4}) $/gi
Msg = "Incorrect verification code format"
Return regExpCheck (re, getO, msg)
}
// Verify the number 1
Function checkNumber (getO)
{
Re =/^ \ d + $/gi
Msg = "format error. Enter 0 ~ 9 digit"
Return regExpCheck (re, getO, msg)
}
// Verify number 2
Function checkNumberText (get0, msg0)
{
Re =/^ \ d + $/gi
// Msg = msg0
Return regExpCheck (re, get0, msg0)
}

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.