A few useful JS functions

Source: Internet
Author: User
js| function//judge is not number create by qiujy
function Isnumber (str) {
var flag = true;

for (i = 0; i < str.length; i++) {
var ch = elementvalue.charat (i);
if (Ch > ' 9 ' | | | ch < ' 0 ') {
Flag = false;
Break
}
}

return flag;
}

To determine if it is empty
function IsEmpty (str) {
if (Str.length = = 0) {
return true;
}
return false;
}

For select, verify that it must have a value option selected, otherwise return false
function Checkselect (SEL) {
var flag = false;
for (i = 0;i < sel.options.length;i++) {
var op = sel.options[i];
if (Op.value!= "" && op.selected = = True) {
Flag = true;
}
}
return flag;
}

For multiple checkbox, verify that it must have at least one selected, otherwise return false
function Checkcheckbox (che) {
var result = false;
if ((typeof che.length) = = "Number") {
for (i = 0;i < Che.length; i++) {
if (che[i].checked) {
return true;
}
}
}else{
Alert ("ok!");
alert (che.checked);
if (che.checked) {
return true;
}
}
return result;
}

Verify the validity of the set password
Call the sample
<input type= "text" name= "UserName" >
<input type= "text" name= "userName2" >
<input name= "Submit2" type= "button" class= "NIU1" value= "Detection" >

function Checkpassword (STR1,STR2) {
if (Str1.length = = 0) {
Alert ("Password cannot be blank");
return false;
}
if (Str1.length > | | str1.length < 4) {
Alert ("Please set password length to 4-18 bits!");
return false;
}
Password verification
if (str1!= str2) {
Alert ("Two input passwords are inconsistent!");
return false;
}
return true;
}

Email format Check
function Checkemailformat (str) {
var reg =/^[_\.0-9a-z-]+@ ([0-9a-z][0-9a-z-]+\.) +[a-z]{2,3}$/;
var result = Str.match (reg);
if (result = = null)
{
Alert (' Please enter the correct email address ');
return false;
}
return true;
}

User name format check, allow numbers, letters, slide line
function Checkuseridformat (str) {
var reg =/\w/;
var result = Str.match (reg);
if (result = = null)
{
return false;
}
return true;
}

Input control
Invoke Sample: <input onkeypress= "Inputcontrl (' digit ')" name = "UserName" >
function Inputcontrl (inputmod) {
Inputmod can be combined input: "Chinese,letter,digit"
if (inputmod== "" | | Inputmod==null) Event.returnvalue=false;
Inputmod=inputmod.tolowercase ();
var gflag = false;

It's only numbers.
if (Inputmod.indexof ("digit")!=-1&&gflag==false)
if (event.keycode>=48&&event.keycode<=57) gflag=true;

can only be Chinese
if (Inputmod.indexof ("Chinese")!=-1&&gflag==false)
if (event.keycode>=256) gflag=true;

Only the letters.
if (Inputmod.indexof ("letter")!=-1&&gflag==false)
if ((event.keycode>=65&&event.keycode<=90) | | (event.keycode>=97&&event.keycode<=122))
Gflag=true;

can only be amount (numbers and decimal points)
if (Inputmod.indexof ("Money")!=-1&&gflag==false)
if (event.keycode<=57 && event.keycode>=48 | | event.keycode==46 | | event.keycode = = 44)
Gflag=true;

Can only be the specified symbol
if (Inputmod.indexof ("symbol")!=-1&&gflag==false) {
Alert ("Running Here");
if (event.keycode== ' _ ' | | | event.keycode== '-')
Gflag=true;
}
Event.returnvalue = Gflag;
}

Check to see if there are any Chinese in the string
function Check_chinese (str) {
for (Var i=0;i<str.length;i++) {
var c=str.charcodeat (i);
if (C > 0x4e00 && C < 0x9fa5) {
Alert ("User name contains Chinese: [" +str.charat (i) + "]");
return false;
}
}
return true;
}

Verify that the string is not the amount of money
function Check_money (str) {
var re =/^ (\d+|[ 1-9]) \. {0,1}\d{0,2}$/;
if (!re.test (str)) {
Alert ("The number of amounts entered is incorrect!") Example: 123 or 123.45 ");
return false;
}
return true;
}

================2006 year February 27 increased ======================

The selected select option moves from the Drop-down list named Srcselect to the Drop-down list named Desselect.

Function Moveoption (srcselect,desselect) {
 srclen = srcSelect.options.length;
 deslen = DesSelect.options.length;
 for (i=0;i<srclen;i++) {
  optionobj = srcselect.options[i];
  if (optionobj.selected) {
   text = Optionobj.text;
   value = Optionobj.value;
   desselect.options[deslen++] = new Option (text,value);
  }
 }
 for (i=srclen-1;i>=0;i--) {
  optionobj = srcselect.options[i];
  if (optionobj.selected) {
   optionobj.selected = false;
   srcselect.options[i] = null;
  }
 }
}

The Select option moves up and down (obj is the ID of Select, index:-1 represents up; 1 means move Down)
function Changepos (obj,index)
{
if (index==-1) {
if (obj.selectedindex>0) {
Obj.options (Obj.selectedindex). SwapNode (Obj.options (obj.selectedindex-1))
}
}
else if (index==1) {
if (obj.selectedindex<obj.options.length-1) {
Obj.options (Obj.selectedindex). SwapNode (Obj.options (obj.selectedindex+1))
}
}
}



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.