ホームページ  >  記事  >  ウェブフロントエンド  >  よく使う簡単なJavaScript関数_JavaScriptスキル

よく使う簡単なJavaScript関数_JavaScriptスキル

WBOY
WBOYオリジナル
2016-05-16 18:54:23854ブラウズ

//関数名: strByteLength
//関数の紹介: 文字列のバイト長を返す
//パラメータの説明: str チェックする文字列
//戻り値: 文字列の長さ
function strByteLength (str)
{
var i,sum;
for(i=0;i{ if (( str) .charCodeAt(i)>=0) & (str.charCodeAt(i)sum=sum 1; else
sum=sum 2;
return sum;
}
//関数名: fucCheckLength
//関数紹介: フォームが指定された長さを満たしているかチェック
//パラメータの説明: obj チェック対象のフォームオブジェクト
/ / name オブジェクト名
// 指定した長さ
// 戻り値: true (一致する) または false (一致しない)
function fucCheckLength(obj , name , length)
{
var i, sum;
var strTemp = obj.value;
for(i=0;iif ((strTemp) .charCodeAt(i)>=0) & (strTemp.charCodeAt(i)else
sum=sum 2; if (sum{ return true;
}
else
{
alert(name "指定された長さを超えています! 許可される最大文字数は " length " 文字です (中国語)は 2 桁としてカウントされます) ! "); obj.focus();
return false;
}
}
//メールが正当かどうかを確認します
function checkEmail(オブジェクト)
{
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("メールが無効です。再入力してください。");
Object.focus();
return false;
}else{
return true;
}
}
//削除関数
function Jtrim (str){
var i = 0;
var len = str.length;
if ( str == "" ) return( str ); flagbegin = true;
flagend = true;
while ( flagbegin == true & iif ( str.charAt(i) == " " ){
i= i 1 ; flagbegin=true;
}else{
flagbegin=false;
}
while (flagend== true & j>=0){
str.charAt(j)==" "){
j=j-1;
}else{
flagend=false; 🎜>if ( i > j ) return ("")
trimstr = str.substring(i,j 1);
returnrimstr;
}
//関数名: JtrimCn
//関数紹介: 文字列の前後のスペースを削除(中国語のスペースを含む)
//パラメータの説明: str 演算対象の文字列
//戻り値: 前後のスペースを削除(中国語のスペースを含む) ) String
function JtrimCn(str){
var i = 0;
if (str == null || str == unknown) {
return "";
var len = str.length;
if ( str == "" ) {
return( str );
j = len -1;
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;
}else; { flagend=false;
}
}
if ( i > j ) {
return ("")
}
varrimstr = str.substring(i, j 1);
returnrimstr;
}
//0-9、A-Z、a-z 標準文字判定
function isChar(Str){
var regu = "^([0- 9a-zA-Z] )$";
var re = new RegExp(regu);
if (Str.search(re) != -1){
return true;
}
return false;
}
//数値かどうかを判定します
function IsNum(Str){
var regu = "^([0-9] )$"; var re = new RegExp(regu);
if (Str.search(re) != -1)
return true;
{
return false; 🎜>/ /関数名: funcIsNotEmpty
//関数紹介: 文字列が空かどうかチェック
//パラメータ説明: str string
//戻り値: true: 空でない false: 空
function funcIsNotEmpty(str){
var s = /S/;
if(str==null){
return false;
return s.test(str); >}
//関数名: fucCheckLength
//関数紹介: フォームが指定された長さを満たしているかチェック
//パラメータ説明: objValue チェック対象のフォームオブジェクトの値
// name オブジェクト名
//minLen 最小長
// maxLen 最大長
//戻り値: true (一致する) または false (一致しない)
function fucCheckLengthB(objValue, minLen, maxLen)
{
var i,sum;
var strTemp = objValue;
for(i=0;i >if ((strTemp .charCodeAt(i)>=0) & (strTemp.charCodeAt(i)else
sum=sum 2; }
if (sum
= minLen)
{
return true;
}
else
{
return false; 🎜>//sDate1 と sDate2 は 2002-12-18 の形式です
function funDateDiff(sDate1, sDate2){
var aDate, oDate1, oDate2, iDays;
aDate = sDate1.split("-" );
/ /2002 年 12 月 18 日の形式に変換します
oDate1 = new Date(aDate[1] '-' aDate[2] '-' aDate[0]); split("-") ;
oDate2 = new Date(aDate[1] '-' aDate[2] '-' aDate[0]) ;
//ミリ秒単位の差を日数に変換します。 iDays = parseInt(Math .abs(oDate1 - oDate2) / 1000 / 60 / 60 /24);
//開始時刻が終了時刻より小さい場合 if (sDate1 > sDate2)
{ return (-1 * iDays );
}
return iDays;
}
//メールが正当なものかどうかを確認しますfunction funcCheckEmail(strValue)
{
var パターン = /^[.-_A -Za-z0-9] @([-_A-Za-z0-9] .) [A-Za-z0-9]{2,3}$/; >if(strValue.match(pattern)= =null){
return false;
}else{
return true;
}
}
//関数名: fucCheckMaxLength
//関数紹介: フォームが指定された長さであるかどうかを確認します
//パラメータの説明: objValue チェックするフォームオブジェクトの値
// 名前オブジェクトの値
// maxLen 最大長
//戻り値: true (適合) または false (一致しない)
function fucCheckMaxLength(objValue , maxLen)
{
return fucCheckLengthB(objValue, 0 ,maxLen )
; >//関数名: fucCheckMaxLength
//関数紹介: 指定されたオブジェクトの値が指定された長さを満たしているかチェック
// パラメータ説明: objValue チェック対象のフォームオブジェクトの値
// name object
// maxLen の最大長
// 戻り値: true (一致する) または false (一致しない)
function fucCheckObjMaxLength(obj , maxLen)
{
if (obj = = null) {
return false;
}
return fucCheckLengthB( obj.value, 0 ,maxLen );
}
//関数名: funcCheckInvalidChar
//関数の紹介:指定されたフィールドに不正な文字が含まれているかどうかを判断します

//パラメータの説明: obj チェック対象のフォームオブジェクト
//戻り値: true (いいえ) または false (はい)
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;
}
/**
* Determine whether the maximum length of the object with 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 "You can only enter at most" (maxLength/ 2) "Chinese characters (or " maxLength "English digits)");
} else {
alert(showMsg);
}
if (needFocus) {
obj.focus( );
}
return false;
}
return true;
}
/**
* Determine whether the object with the specified ID contains illegal characters
* param: objId object ID
* objName name of the object
* needFocus whether focus needs to be set
* showMsg Error message displayed
* return: true is correct, false is 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 ""');
} else {
alert(showMsg);
}
if (needFocus) {
obj.focus();
}
return false;
}
return true;
}
/**
* Determine whether the object with the specified ID is empty
* param: objId object ID
* objName name of the object
* needFocus whether focus needs to be set
* showMsg Error message displayed
* return: true is not empty, false is empty
*/
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)month = "0" month;
if(Number(day)day = "0" day;
var retDate = newDate.getYear() "-" month "-" day;
return retDate;
}

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。