javascript検証
24813 ワード
/*
1. endsWith(theStr,endStr)
@param theStr:
@param endStr:
@return boolean;
*/
function endsWith(theStr,endStr)
{
var theStrLength=theStr.length;
var endStrLength=endStr.length;
var theStrEnd=theStr.substring(theStrLength-endStrLength,theStrLength);
if (theStrEnd==endStr)
return true;
else
return false;
}
/*
1. startsWith(theStr,endStr)
@param theStr:
@param endStr:
@return boolean;
*/
function startsWith(theStr,startStr)
{
var theStrLength=theStr.length;
var startStrLength=startStr.length;
var theStrStart=theStr.substring(0,startStrLength);
if (theStrStart==startStr)
return true;
else
return false;
}
/*
2.
@param str:
@param bEmpty: str
*/
function isInt(str,bEmpty)
{
if(str==null||trim(str)=="")
{
return bEmpty;
}
if(trim(str) == "0")
{
return false;
}
var ch = str.substring(0,1);
if (ch == "0" && trim(str) != "0")
{
return false;
}
for (var i=0; i<str.length ; i++)
{
var s = str.substring(i, i+1);
if (!(s >= "0" && s <="9"))
{
return false;
}
}
return true;
}
/*
3、
@param Str:
@param Flag: , l L ; r R ;
*/
function trim(Str , Flag)
{
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
RegularExp = /^\s+/gi;
return Str.replace( RegularExp,"" );
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
RegularExp = /\s+$/gi;
return Str.replace( RegularExp,"" );
}
else/*defautly, trim both left and right side*/
{
RegularExp = /^\s+|\s+$/gi;
return Str.replace( RegularExp,"" );
}
}
/*
4、
@param checkstr:
@param bEmpty: checkstr
@param tcount:
*/
function isFloat(checkstr, bEmpty, tcount)
{
if(checkstr==null||trim(checkstr)=="")
{
return bEmpty;
}
var str = trim(checkstr);
if(str.substring(0,1)==".")
{
return false;
}
var temp=0;
for(var i=0;i<str.length;i++)
{
var ch=str.substring(i,i+1);
if(!((ch>="0" && ch<="9") || ch=="."))
{
return false;
}
if(ch==".")
temp++;
if(temp>1)
{
return false;
}
}
if(tcount != null && tcount > 0)
{
if(str.indexOf(".") != -1 && str.length - (str.indexOf(".")+1) > tcount)
{
return false;
}
}
var start1 = checkstr.substring(0,1);
var start2 = checkstr.substring(1,2);
if(start1 == 0 && start2!=".")
{
for(var i=0;i<str.length;i++)
{
var ch=str.substring(i,i+1);
if (ch==0)
temp++;
}
if (temp == str.length)
{
return true;
}
return false;
}
return true;
}
/*
5、
@param str:
@param bEmpty:str
*/
function isNumber(str,bEmpty)
{
if(str==null||trim(str)=="")
{
return bEmpty;
}
for (var i=0; i<str.length ; i++)
{
var s = str.substring(i, i+1);
if (!(s >= "0" && s <="9"))
{
return false;
}
}
return true;
}
/*
6、
@param str:
*/
function isNull(str)
{
if (str == null || trim(str) == "")
{
return true;
}
return false;
}
/*
8、 EMAIL
@param str:
@param bEmpty:str
*/
function isEmail(str,bEmpty){
if (str == null || trim(str) == "")
{
return bEmpty;
}
// , [email protected] [email protected]
var pattern1 = /^(([\u4E00-\u9FA5]|[\uFE30-\uFEFF]|[\d]|[a-zA-Z]|[\_\-\.])+[\@])(((([\d]|[a-zA-Z]|[\_\-]|[\[\]\{\}\>\<])+[\.])+)([\d]|[a-zA-Z\_\-])+)$/;
if(str.match(pattern1) )
return true;
return false;
}
/*
9、
@param str:
@param bEmpty:str
*/
function isTel(str,bEmpty)
{
if(str==null||trim(str)=="")
return bEmpty;
var str = trim(str);
var myphone = /^[\-0-9+()\/]{1,32}$/;
if(myphone.test(str)) return true;
return false;
}
//-- - x
function isIdCardNo(num)
{
var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var error;
var varArray = new Array();
var intValue;
var lngProduct = 0;
var intCheckDigit;
var intStrLen = num.length;
var idNumber = num;
var checkDate = function(date) {
return true;
}
// initialize
if ((intStrLen != 15) && (intStrLen != 18)) {
//error = " !";
//alert(error);
//frmAddUser.txtIDCard.focus();
return false;
}
// check and set value
for(i=0;i<intStrLen;i++) {
varArray[i] = idNumber.charAt(i);
if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
//error = " !.";
//alert(error);
//frmAddUser.txtIDCard.focus();
return false;
} else if (i < 17) {
varArray[i] = varArray[i]*factorArr[i];
}
}
if (intStrLen == 18) {
//check date
var date8 = idNumber.substring(6,14);
if (checkDate(date8) == false) {
//error = " !.";
//alert(error);
return false;
}
// calculate the sum of the products
for(i=0;i<17;i++) {
lngProduct = lngProduct + varArray[i];
}
// calculate the check digit
intCheckDigit = 12 - lngProduct % 11;
switch (intCheckDigit) {
case 10:
intCheckDigit = 'X';
break;
case 11:
intCheckDigit = 0;
break;
case 12:
intCheckDigit = 1;
break;
}
// check last digit
if (varArray[17].toUpperCase() != intCheckDigit) {
//error = " ! : " + intCheckDigit + ".";
//alert(error);
return false;
}
}
else{ //length is 15
//check date
var date6 = idNumber.substring(6,12);
if (checkDate(date6) == false) {
//alert(" !.");
return false;
}
}
//alert ("Correct.");
return true;
}
/*
*/
function strDateTime(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
function replaceAll(str,replaceStr,toStr){
var raRegExp = new RegExp(replaceStr.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
return str.replace(raRegExp,toStr);
}
function url(str){
str=replaceAll(str,"?","$param$");
str=replaceAll(str,"&","$and$");
str=replaceAll(str,"%","$percent$");
return str;
}