Common JavaScript function code _javascript tips

Source: Internet
Author: User
Mhhover (' Start ID name ', ' X 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, show hidden ID
How to use
Onchange= "Optionnumber_change (' form name ', ' ID name same part (sequence number automatic value) ')"
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 = j > A; j--)
{
document.getElementById (stropt+j). Style.display = "None";
}
}

Read the database, show hidden IDs
How to use
//

Program code

function Listnumber_change (LISTOBJ,LISTSTR)
{
var a = Listobj;
for (i = 1; i <= A; i++)
{
document.getElementById (liststr+i). Style.display = "";
}
for (j = j > A; j--)
{
document.getElementById (liststr+j). Style.display = "None";
}
}

Self-adaptive height of IFRAME
How to use:
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
How to use:
Onclick=checkall (this.form,this.checked)

Program code

function Checkall (form1,v) {
for (Var i=0;i<form1.elements.length;i++) {
var e = form1.elements[i];
if (e.type== ' checkbox ') e.checked = v;
}
}

Verifying the correctness of form string expressions

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;
}

Verifying user Names

Program code

function CheckAccount (Geto)
{
Re=/^ ([a-z0-9]{4,12}) $/gi
msg = "username format: 4-12 lowercase (A-Z) digits (0-9)"
Return Regexpcheck (RE,GETO,MSG)
}

Verify Password

Program code

function Checkpassy (Geto)
{
Re=/^ ([a-za-z0-9]{8,12}) $/gi
msg = "Login password format: 8-12-digit English (a-z,a-z) number (0-9)"
Return Regexpcheck (RE,GETO,MSG)
}

Verify real name

Program code

function Checknamen (Geto)
{
Re=/^[^\ ' \,\;\$\|\@\*]{2,10}$/gi
msg = "real name format error"
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 = Gv.length-gv_c
Gv_len = gv_c*2 + gv_e
if (gv_len>20)
{
Alert ("cannot be longer than 10 characters")
return false;
}
Else
return True
}
}
Else
return false;
}

Verify ID Number

Program code

function Checkidcard (Geto)
{
Re=/^ ([0-9]{15}) | ( [0-9a-z] {18})) $/gi;
msg = "ID card format error"
Return Regexpcheck (RE,GETO,MSG)
}

Verifying e-mail

Program code

function Checkemail (Geto)
{
Re=/^ ([\.\w-]) {2,30}@ ([\w-]) {3,30} (\. [\w]) {2,4}) {1,2}/gi
msg = "Mailbox Format error"
Return Regexpcheck (RE,GETO,MSG)
}

Verify 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)
}

Verifying phone numbers

Program code

function Checkphone (Geto)
{
Re=/^ ([\d]) {2,5}-([\d]) {3,14}/gi
msg = "Phone format error \r\n\r\n Area number and telephone number in the middle please use"-"lattice open, for example: 029-82303721"
Return Regexpcheck (RE,GETO,MSG)
}

Verify your phone number

Program code

function Checkmobile (Geto)
{
Re=/^ (\+?[ 0-9]{0,3}13[0-9]{9}) $/gi
msg = "Phone format error"
Return Regexpcheck (RE,GETO,MSG)
}

Verifying the Checksum code

Program code

function Checkvcode (Geto)
{
Re=/^ (\d{4}) $/gi
msg = "Bad Check code format"
Return Regexpcheck (RE,GETO,MSG)
}
Verify Number 1
function CheckNumber (Geto)
{
Re=/^\d+$/gi
msg = "Wrong format, please enter 0~9 number"
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.