Common simple JavaScript function _javascript techniques

Source: Internet
Author: User
Tags getdate
Function Name: strbytelength
Feature Description: Returns the byte length of a string
Parameter description: str to check the string
return Value: String length
function Strbytelength (str)
{
var i,sum;
sum=0;
for (i=0;i<str.length;i++)
{
if ((Str.charcodeat (i) >=0) & (Str.charcodeat (i) <=255))
sum=sum+1;
Else
sum=sum+2;
}
return sum;
}
Function Name: fucchecklength
Function Description: Check whether the form meets the specified length
Parameter description: obj to check the form object
Name object names
Length specified lengths
return value: TRUE (compliant) or false (inconsistent)
function fucchecklength (obj, name, length)
{
var i,sum;
sum=0;
var strtemp = Obj.value;
for (i=0;i<strtemp.length;i++)
{
if ((Strtemp.charcodeat (i) >=0) & (Strtemp.charcodeat (i) <=255))
sum=sum+1;
Else
sum=sum+2;
}
if (sum<=length)
{
return true;
}
Else
{
Alert (name+) exceeds the specified length! Maximum allowable "+length+" character (Chinese count 2 digits)! ");
Obj.focus ();
return false;
}
}
Detect if e-mail is legal
function Checkemail (Object)
{
var pattern =/^[.-_a-za-z0-9]+@ ([-_a-za-z0-9]+\.) +[a-za-z0-9]{2,3}$/;
var Strvalue=object.value;
if (Strvalue.match (pattern) ==null) {
Alert ("Email is not valid, please fill in again!") ");
Object.focus ();
return false;
}else{
return true;
}
}
Go to the empty partition function
function Jtrim (str) {
var i = 0;
var len = str.length;
if (str = = "") return (str);
j = len-1;
Flagbegin = true;
Flagend = true;
while (Flagbegin = = True & i< len) {
if (Str.charat (i) = = "") {
i=i+1;
Flagbegin=true;
}else{
Flagbegin=false;
}
}
while (flagend== True & j>=0) {
if (Str.charat (j) = "") {
J=j-1;
Flagend=true;
}else{
Flagend=false;
}
}
if (i > J) return ("")
Trimstr = str.substring (i,j+1);
return trimstr;
}
Function Name: JTRIMCN
Feature Description: Remove the space before and after the string [including Chinese spaces]
Parameter description: str the string to manipulate
Return value: A string that has been removed before and after spaces [including Chinese spaces]
function JTRIMCN (str) {
var i = 0;
if (str = NULL | | | str = undefined) {
Return "";
}
var len = str.length;
if (str = = "") {
return (str);
}
j = len-1;
Flagbegin = true;
Flagend = true;
while (Flagbegin = = True & i< len) {
if (Str.charat (i) = = "" | | Str.charat (i) = = "") {
i=i+1;
Flagbegin=true;
}else{
Flagbegin=false;
}
}
while (flagend== True & j>=0) {
if (Str.charat (j) = = "" | | Str.charat (j) = = "") {
J=j-1;
Flagend=true;
}else{
Flagend=false;
}
}
if (i > J) {
Return ("")
}
var trimstr = str.substring (i,j+1);
return trimstr;
}
0-9,A-Z,A-Z Specification character judgment
function Ischar (STR) {
var Regu = "^ ([0-9a-za-z]+) $";
var re = new RegExp (Regu);
if (Str.search (re)!=-1) {
return true;
}
return false;
}
Judge whether the number
function Isnum (STR) {
var Regu = "^ ([0-9]+) $";
var re = new RegExp (Regu);
if (Str.search (re)!=-1)
return true;
{
return false;
}
}
Function Name: Funcisnotempty
Feature Description: Check whether the string is empty
Parameter description: str string
Return value: true: null false: null
function Funcisnotempty (str) {
var s =/\s/;
if (str==null) {
return false;
}
return S.test (str);
}
Function Name: fucchecklength
Function Description: Check whether the form meets the specified length
Parameter description: ObjValue The value of the form object to check
Name object names
Minlen Minimum Length
MaxLen Maximum Length
return value: TRUE (compliant) or false (inconsistent)
function FUCCHECKLENGTHB (ObjValue, Minlen, MaxLen)
{
var i,sum;
sum=0;
var strtemp = ObjValue;
for (i=0;i<strtemp.length;i++)
{
if ((Strtemp.charcodeat (i) >=0) & (Strtemp.charcodeat (i) <=255))
sum=sum+1;
Else
sum=sum+2;
}
if (Sum<=maxlen & sum >= minlen)
{
return true;
}
Else
{
return false;
}
}
SDate1 and SDate2 are the 2002-12-18 format
function Fundatediff (sDate1, SDate2) {
var adate, oDate1, ODate2, idays;
Adate = Sdate1.split ("-");
Convert to 12-18-2002 format
oDate1 = new Date (adate[1] + '-' + adate[2] + '-' + adate[0 ');
Adate = Sdate2.split ("-");
ODate2 = new Date (adate[1] + '-' + adate[2] + '-' + adate[0 ');
Converts the difference in milliseconds to days
Idays = parseint (Math.Abs (odate1-odate2)/1000/60/60/24);
If the start time is less than the end time
if (SDate1 > SDate2)
{
Return ( -1 * idays);
}
return idays;
}
Detect if e-mail is legal
function Funccheckemail (strvalue)
{
var pattern =/^[.-_a-za-z0-9]+@ ([-_a-za-z0-9]+\.) +[a-za-z0-9]{2,3}$/;
if (Strvalue.match (pattern) ==null) {
return false;
}else{
return true;
}
}
Function Name: fuccheckmaxlength
Function Description: Check whether the form meets the specified length
Parameter description: ObjValue The value of the form object to check
Name Object value
MaxLen Maximum Length
return value: TRUE (compliant) or false (inconsistent)
function Fuccheckmaxlength (ObjValue, MaxLen)
{
Return FUCCHECKLENGTHB (objvalue, 0, MaxLen);
}
Function Name: fuccheckmaxlength
Function Description: Check the value of the specified object to meet the prescribed length
Parameter description: ObjValue The value of the form object to check
Name Object
MaxLen Maximum Length
return value: TRUE (compliant) or false (inconsistent)
function fuccheckobjmaxlength (obj, maxlen)
{
if (obj = = null) {
return false;
}
Return FUCCHECKLENGTHB (obj.value, 0, MaxLen);
}
Function Name: Funccheckinvalidchar
Function Description: Determine whether the specified field has illegal characters <>
Parameter description: obj to check the form object
return value: True (not) or false (with)
function Funccheckinvalidchar (obj)
{
if (obj = null | | obj.value== "")
{
return true;
}
alert (Obj.value);
var pattern =/[<>]/;
if (Pattern.test (Obj.value)) {
return false;
}else{
return true;
}
}
/**
* Determines whether the maximum length of the object for the specified ID is correct
* Param:objid Object ID
* MaxLength Maximum length
* Return:true correct, false incorrect
*/
function lengthmaxcheckmsg (ObjID, MaxLength, objname, Needfocus, showmsg) {
Personal information check
var obj = document.getElementById (ObjID);
if (!fuccheckobjmaxlength (obj, maxLength)) {
if (showmsg = null | | showmsg== "") {
Alert (objname + "can only enter" + (MAXLENGTH/2) + "kanji (or" + maxLength + "English digit)");
} else {
alert (showmsg);
}
if (Needfocus) {
Obj.focus ();
}
return false;
}
return true;
}
/**
* Determine if the object of the specified ID contains illegal characters
* Param:objid Object ID
* Name of the ObjName object
* Whether the needfocus need to set the focus
* ShowMsg the error message displayed
* Return:true correct, false incorrect
*/
function invalidcharcheckmsg (ObjID, Objname,needfocus, showmsg) {
Personal information check
var obj = document.getElementById (ObjID);
if (!funccheckinvalidchar (obj)) {
if (showmsg = null | | showmsg== "") {
Alert (objname + ' cannot contain "<" or ">");
} else {
alert (showmsg);
}
if (Needfocus) {
Obj.focus ();
}
return false;
}
return true;
}
/**
* Determine if the object of the specified ID is empty
* Param:objid Object ID
* Name of the ObjName object
* Whether the needfocus need to set the focus
* ShowMsg the error message displayed
* Return:true is not NULL, FALSE is NULL
*/
function emptycheckmsg (ObjID, Objname,needfocus, showmsg) {
Personal information check
var obj = document.getElementById (ObjID);
if (!funcisnotempty (Obj.value)) {
if (showmsg = null | | showmsg== "") {
Alert (objname + ' cannot be empty!) ');
} else {
alert (showmsg);
}
if (Needfocus) {
Obj.focus ();
}
return false;
}
return true;
}
/* Date Calculation function
* Date 2007-01-01
* CNT 1 Or-1
* Return 2007-01-02
*/
function getDate (date, CNT) {
if (date.length!=10) {
Return "";
}
var Datearray = Date.split ("-")
if (datearray==null) {
Return "";
}
var temdate = new Date (datearray[0], datearray[1]-1, datearray[2]);
var newdate = DATE.UTC (Temdate.getyear (), Temdate.getmonth (), Temdate.getdate ())
Newdate = Newdate + (cnt*24*60*60*1000);
Newdate = new Date (newdate);
var month = Newdate.getmonth () +1;
var day = Newdate.getdate ();
if (number (month) <10)
month = "0" +month;
if (number (day) <10)
Day = "0" +day;
var retdate = newdate.getyear () + "-" + month + "-" + day;
return retdate;
}
Related Article

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.