常用的表单验证及一些字符串转义函数--JS

// 是否为空,非空返回真,不非为空返回假
function isBlank(str) { var blankFlag = true;
   
if (str.length == 0) return true;
   
for (var i = 0; i < str.length; i++) {
       
if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
            blankFlag
= false;
           
break;
        }
    }
   
return blankFlag;
}
function checkNotNull(theField, fieldName) {
   
if(isBlank(theField.value)){
        alert(fieldName
+ "不可为空!");
               
if(theField.type!="hidden"){
          theField.focus();
                }
       
return false;
    }
   
return true;
}

// 是否为数字
function checkNumber(theField, fieldName) {
    
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
    
if(theField.value.trim() == "") return true;
    
if (!pattern.test(theField.value.trim())) {
         alert(fieldName
+ "必须为合法数字");
         theField.focus();
         theField.select();
        
return false;
     }

   
return true;
}

function isNumber(str) {
 
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
 
if(str.trim() == "") return false;
 
if (!pattern.test(str.trim())) return false;
 
return true;
}

// 是否为指定范围数字
function checkNumberRange(theField, fieldName, min, max) {
   
if(theField.value.trim() == "") return true;
   
if (!checkNumber(theField, fieldName)) return false;

   
if ((min != "") && (theField.value.trim() < min)) {
        alert(fieldName
+ "不可小于" + min + "");
        theField.focus();
        theField.select();
       
return false;
    }

   
if ((max != "") && (theField.value.trim() > max)) {
        alert(fieldName
+ "不可超过" + max + "");
        theField.focus();
        theField.select();
       
return false;
    }

   
return true;
}

function isNumberRange(str, min, max) {
if(str == "") return false;
   
if (!isNumber(str)) return false;

   
if ((min != "") && (str < min)) {
       
return false;
    }

   
if ((max != "") && (str > max)) {
       
return false;
    }

   
return true;
}

// 是否为整数
function checkInteger(theField, fieldName) {
   
var pattern = /^(/d|(-/d))/d*$/;

   
if(theField.value.trim() == "") return true;
   
if (!pattern.test(theField.value.trim())) {
        alert(fieldName
+ "必须为整数!");
        theField.focus();
        theField.select();
       
return false;
    }

   
return true;
}

function isInteger(str) {
   
var pattern = /^(/d|(-/d))/d*$/;

   
if(str.trim() == "") return false;
   
if (!pattern.test(str.trim())) {
       
return false;
    }
   
return true;
}

// 是否为指定范围内整数
function checkIntegerRange(theField, fieldName, min, max) {
   
if(theField.value.trim() == "") return true;
   
if (!checkInteger(theField, fieldName)) return false;

   
if ((min != "") && (theField.value.trim() < min)) {
        alert(fieldName
+ "不可小于" + min + "");
        theField.focus();
        theField.select();
       
return false;
    }

   
if ((max != "") && (theField.value.trim() > max)) {
        alert(fieldName
+ "不可超过" + max + "");
        theField.focus();
        theField.select();
       
return false;
    }

   
return true;
}

function isIntegerRange(str, min, max) {
   
if(str == "") return false;
   
if (!isInteger(str)) return false;

   
if ((min != "") && (str < min)) {
       
return false;
    }

   
if ((max != "") && (str > max)) {
       
return false;
    }

   
return true;
}

// 是否为正数
function checkPositiveNumber(theField, fieldName) {
   
if(theField.value == "") return true;
   
if (theField.value.charAt(0) == '-') {
        alert(fieldName
+ "必须为正数!");
        theField.focus();
       
return false;
    }

   
return true;
}

 

// 电子邮件验证
function checkEmail(theField) {
   
var pattern = /^.+@.+/..+$/;

   
if(theField.value == "") return true;
   
if (!pattern.test(theField.value)) {
        alert(
"请输入合法的电子邮件地址");
        theField.focus();
        theField.select();
       
return false;
    }

   
return true;
}

// 是否为只读域(如file,text等域只接受右边按钮选择传回的结果)
function checkReadField() {
    alert(
"请点击后面的图标进行选择");
   
// this.blur();
}
/*
*    RoundTo(Digit,How):数值格式化函数,Digit要格式化的数字,How要保留的小数位数。
*/
function  RoundTo(Digit,How)
{
   Digit 
=  Math.round(Digit*Math.pow(10,How))/Math.pow(10,How);
   return  Digit;
}
//去除字符串的前后空格
String.prototype.trim = function()
{
  
return this.replace(/(^/s+)|/s+$/g,"");
}

//将指定的字段转换为大写
function UpperCase(theField){
  theField.value
= theField.value.toUpperCase();
}

//将指定的字段转换为小写
function LowerCase(theField){
  theField.value
= theField.value.toLowerCase();
}

//比较两个时间大小,相等或第二个大返回true,第一个大返回false.
//
现在只支持2005-12-30或2005-12-30 10:00:00的时间格式
function DateCompare(theField1,fieldName1,theField2,fieldName2)
{
 
var d1 = theField1.value;
 
var d2 = theField2.value;
 
if (d1==d2) return true;    // 相等
  var a1 = null;var a2 = null;
 
var b1 = null;var b2 = null;
 
if (d1.length==10){
    d1
+= " 00:00:00";
  }
  a1
=d1.substring(0,10).split("-");
  a2
=d1.substring(11).split(":");
 
if (d2.length==10){
    d2
+= " 00:00:00";
  }
  b1
=d2.substring(0,10).split("-");
  b2
=d2.substring(11).split(":");

 
for (i=0;i<3;i++){
   
if( a1[i].charAt(0) == '0' ) { a1[i] = a1[i].substring(1,2); }
   
if( b1[i].charAt(0) == '0' ) { b1[i] = b1[i].substring(1,2); }
   
if (parseInt(a1[i])>parseInt(b1[i])){
      alert(fieldName1
+"不能大于"+fieldName2);
     
return false;
    }
   
if (parseInt(a1[i])<parseInt(b1[i])){
     
return true;
    }
  }
 
for (i=0;i<3;i++){
   
if( a2[i].charAt(0) == '0' ) { a2[i] = a2[i].substring(1,2); }
   
if( b2[i].charAt(0) == '0' ) { b2[i] = b2[i].substring(1,2); }
   
if (parseInt(a2[i])>parseInt(b2[i])){
      alert(fieldName1
+"不能大于"+fieldName2);
     
return false;
    }
   
if (parseInt(a2[i])<parseInt(b2[i])){
     
return true;
    }
  }
}

//将给定的字符串中的&字符替换成@@
function ConvertStr(s){
 
var i;
 
var s2 = s;

 
while(s2.indexOf("&")>0){
     i
= s2.indexOf("&");
     s2
= s2.substring(0, i) + "@@" + s2.substring(i + 1, s2.length);
  }
 
return s2;
}

//将给定的字符串中的@@字符替换成&
function ReConvertStr(s){
 
var i;
 
var s2 = s;

 
while(s2.indexOf("@@")>0){
     i
= s2.indexOf("@@");
     s2
= s2.substring(0, i) + "&" + s2.substring(i + 2, s2.length);
  }
 
return s2;
}
//将给定的字符串中的单双引号转义
function ForamtValue(oStr)
{
   
switch(typeof(oStr))
    {
       
case "date"     :
           
//直接toString()转换,可以加入丰富的显示方式
            sStr = (new Date(oStr)).toString();
           
break;
       
default         :
            sStr
= String(oStr);
    }
    sStr
= sStr.replace(//"/g,"&#34;"); //输入框中显示双引号问题
    sStr = sStr.replace(//'/g,"&#39;"); //输入框中显示单引号问题
    return sStr;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值