Home >Web Front-end >JS Tutorial >Commonly used simple JavaScript functions_javascript skills

Commonly used simple JavaScript functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:54:23923browse

//Function name: strByteLength
//Function introduction: Return the byte length of the string
//Parameter description: str The string to be checked
//Return value: String length
function strByteLength(str)
{
var i,sum;
sum=0;
for(i=0;i{
if (( str.charCodeAt(i)>=0) & (str.charCodeAt(i)sum=sum 1;
else
sum=sum 2;
}
return sum;
}
//Function name: fucCheckLength
//Function introduction: Check whether the form meets the specified length
//Parameter description: obj The form object to be checked
// name object name
// length specified length
// return value: true (matches) or false (does not match)
function fucCheckLength(obj , name , length)
{
var i, sum;
sum=0;
var strTemp = obj.value;
for(i=0;i{
if ((strTemp.charCodeAt( i)>=0) & (strTemp.charCodeAt(i)sum=sum 1;
else
sum=sum 2;
}
if(sum{
return true;
}
else
{
alert(name "exceeds the specified length! The maximum allowed is " length " characters (Chinese is counted as 2 digits) ! ");
obj.focus();
return false;
}
}
//Check whether the email 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("The email is invalid, please refill it!");
Object.focus();
return false;
}else{
return true;
}
}
//removal 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 & iif ( 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
//Function introduction: Remove the spaces before and after the string [including Chinese spaces]
//Parameter description: str The string to be operated
//Return value: Remove the spaces before and after (including Chinese spaces) String
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 & iif ( 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 standard 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;
}
//Determine whether it is a 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
//Function introduction: Check whether the string is empty
//Parameter description: str string
//Return value: true: not empty false: empty
function funcIsNotEmpty(str){
var s = /S/;
if(str==null){
return false;
}
return s.test(str);
}
//Function name: fucCheckLength
//Function introduction: Check whether the form meets the specified length
//Parameter description: objValue The value of the form object to be checked
// name object name
//minLen minimum length
// maxLen maximum length
//return value: true (matches) or false (does not match)
function fucCheckLengthB(objValue, minLen, maxLen)
{
var i,sum;
sum=0;
var strTemp = objValue;
for(i=0;i{
if ((strTemp .charCodeAt(i)>=0) & (strTemp.charCodeAt(i)sum=sum 1;
else
sum=sum 2;
}
if (sum= minLen)
{
return true;
}
else
{
return false;
}
}
//sDate1 and sDate2 is in 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]) ;
//Convert 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;
}
//Check whether the email is legitimate
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 introduction: Check whether the form The specified length
//Parameter description: objValue The value of the form object to be checked
// name object value
// maxLen maximum length
//return value: true (conforming) or false (Does not match)
function fucCheckMaxLength(objValue , maxLen)
{
return fucCheckLengthB(objValue, 0 ,maxLen );
}
//Function name: fucCheckMaxLength
//Function introduction : Check whether the value of the specified object meets the specified length
// Parameter description: objValue The value of the form object to be checked
// name object
// maxLen maximum length
// return value: true (matches) or false (does not match)
function fucCheckObjMaxLength(obj , maxLen)
{
if (obj == null) {
return false;
}
return fucCheckLengthB( obj.value, 0 ,maxLen );
}
//Function name: funcCheckInvalidChar
//Function introduction: Determine whether the specified field has illegal characters
//Parameter description: obj Form object to be checked
//Return value: true (no) or false (yes)
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;
}
}
/**
* 지정된 ID를 가진 객체의 최대 길이가 올바른지 확인
* param: objId 객체 ID
* maxLength 최대 길이
* 반환: true 정답, false 부정확
*/
function lengthMaxCheckMsg(objId, maxLength, objName, needFocus, showMsg) {
//개인정보 확인
var obj = document.getElementById( objId);
if (!fucCheckObjMaxLength(obj, maxLength)) {
if (showMsg == null || showMsg== "") {
alert(objName "최대 개수만 입력할 수 있습니다." ( maxLength/ 2) "한자(또는 " maxLength "영문 숫자)");
} else {
alert(showMsg)
}
if (needFocus) {
obj.focus ( );
}
false를 반환합니다.
}
true를 반환합니다.
/**
* 지정된 ID를 가진 개체에 잘못된 문자가 포함되어 있는지 확인
* param: objId 개체 ID
* objName 개체 이름
* needFocus 포커스 설정 필요 여부
* showMsg 오류 표시된 메시지
* 반환: true는 맞음, false는 틀림
*/
functionvalidCharCheckMsg(objId, objName,needFocus, showMsg ) {
//개인정보 확인
var obj = document.getElementById(objId);
if (!funcCheckInvalidChar(obj)) {
if (showMsg == null || showMsg== " ") {
alert(objName '은 "
"'을 포함할 수 없음) } else {
alert(showMsg)
}
if (needFocus) {
obj.focus();
return false;
}
return true
}
/**
* 지정된 ID를 가진 개체가 비어 있는지 확인
* param: objId 개체 ID
* objName 개체 이름
* needFocus 포커스 설정 필요 여부
* showMsg 오류 메시지 표시
* 반환: true는 비어 있지 않으며, false는 비어 있습니다
*/
functionemptyCheckMsg(objId, objName ,needFocus, showMsg) {
//개인정보 확인
var obj = document.getElementById(objId);
if (!funcIsNotEmpty(obj.value)) {
if (showMsg == null || showMsg== "") {
alert(objName '은 비워둘 수 없습니다! ');
} else {
alert(showMsg);
}
if (needFocus) {
obj.focus();
return false; >}
return true;
}
/*날짜 계산 함수
* date 2007-01-01
* cnt 1 또는 -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)월 = "0" 월
if(Number(day)day = "0" 일
var retDate = newDate.getYear() "-" 월 "-" 일; >retDate 반환
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn