C#版

         /// <summary>
         /// 摘要:
         ///     验证是否是数字【包括正数,负数,附:12,12.345,-0.16,+1234.567】
         /// </summary>
         /// <param name="str">要验证的字符串</param>
         /// <returns>正确返回true,错误返回false</returns>
         public static bool IsNumeric(string str)
         {
             return System.Text.RegularExpressions.Regex.IsMatch(str, @"^([+-]?)\d*[.]?\d*$");
         }

 

         /// <summary>
         /// 摘要:
         ///     验证整数
         /// </summary>
         /// <param name="str">要验证的字符串</param>
         /// <returns>正确返回true,错误返回false</returns>
         public static bool IsInt(string str)
         {
             return System.Text.RegularExpressions.Regex.IsMatch(str, @"^-?\d+$");
         }

 

       js trim 正则表达式

       var test = "   sd   das   ";
       String.prototype.trim = function(){        //去出头尾空格
       var re = /^\s+(.*?)\s+$/;
       return this.replace(re,"$1");
       }
       alert("["+test.trim()+"]");

//头尾空格

 String.prototype.Trim = function() { 
   var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/); 
   return (m == null) ? "" : m[1]; 
 }
 //手机
 String.prototype.isMobile = function() { 
   return (/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim())); 
 }
 //电话
 String.prototype.isTel = function()
 {
     //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
     //return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
     return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
 }

 

posted on 2008-12-07 10:01  秦岭过客  阅读(369)  评论(0编辑  收藏  举报