jqueryはidまたはnameでラベルの値を取得し、簡単なjs正規表現(メモ)

4951 ワード


/**
 * (       )        null  ,     true
 * @param str
 * @returns {Boolean}
 */
functionisNullOrEmpty(str)
{
 if(trim(str)==null|| trim(str) == "")
 {
  returntrue;
 }
 returnfalse;
}
 
functioncheckNull(str)
{
 if(str == null|| str == 'null')
 {
  return'';
 }
 returnstr;
}
 
/**
 *           id 
 *    null    
 * @param id
 * @returns {Boolean}
 */
functionnullOrEmpty(id)
{
 varstr = trim(getValue(id));
 $("#"+id).val(trim(getValue(id)));
 if(trim(str)==null|| trim(str) == "")
 {
  returntrue;
 }
 returnfalse;
}
 
/**
 *          
 * @param id
 * @returns
 */
functiontrimSave(id)
{
 varm = trim(getValue(id));
 $("#"+id).val(m);
 returnm;
}
 
/**
 *           
 * @param m
 * @returns
 */
functiontrim(m)
{
  while((m.length>0)&&(m.charAt(0)==' '))  
    m   =   m.substring(1, m.length);   
    while((m.length>0)&&(m.charAt(m.length-1)==' '))  
    m = m.substring(0, m.length-1);   
 returnm;  
}
 
/**
 * @param s
 * @returns {Boolean}
 */
functionisChinese(s){  //          
   varre = newRegExp("[\\u4e00-\\u9fa5]","");//           
   varyesorno = re.test(s);  
   if(yesorno){        
    returntrue; 
   } else{ 
    returnfalse;
   }
  }
  /**
   *           ,    ,     
   * @param str
   * @returns {Number}
   */
  functioncharLength(str)
  {
   varleng = 0;//     
   //                  
   for( vari = 0; i < str.length; i++) {
  if(isChinese(str.charAt(i)))
  {
   leng += 2;
  }else
  {
   leng += 1;
  }
 }
 returnleng;
  }
   
  /**
   *   id   
   * @param id
   */
  functiongetValue(id)
  {
   return$("#"+id).val();
  }
   
   
  /**
   *            
   * @param name
   * @param value
   */
  functionsetRadio(name,value)
  {
   $("input:radio[name="+name+"][value="+value+"]").attr("checked","checked");
  }
   
  /**
   *            ,     
   * @param name
   * @param value
   */
  functionsetCheckbox(name,value)
  {
   $("input:checkbox[name="+name+"][value="+value+"]").attr("checked","checked");
  }
   
  /**
   *   name           
   * @param name
   */
  functiongetRadio(name)
  {
   $("input:radio[name="+name+"]:checked").val();
  }
 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  /**js  **/
   
  varvalidateRegExp = {
      decmal:"^([+-]?)\\d*\\.\\d+$",//   
      decmal1:"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$",//    
      decmal2:"^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$",//    
      decmal3:"^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$",//   
      decmal4:"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$",//     (     + 0)
      decmal5:"^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$",//     (     + 0)
      intege:"^-?[1-9]\\d*$",//  
      intege1:"^[1-9]\\d*$",//   
      intege2:"^-[1-9]\\d*$",//   
      num:"^([+-]?)\\d*\\.?\\d+$",//  
      num1:"^[1-9]\\d*|0$",//  (    + 0)
      num2:"^-[1-9]\\d*|0$",//  (    + 0)
      ascii:"^[\\x00-\\xFF]+$",// ACSII  
      chinese:"^[\\u4e00-\\u9fa5]+$",//   
      color:"^[a-fA-F0-9]{6}$",//  
      date:"^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$",//  
      email:"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",//  
      idcard:"^[1-9]([0-9]{14}|[0-9]{17})$",//   
      ip4:"^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$",//ip  
      letter:"^[A-Za-z]+$",//  
      letter_l:"^[a-z]+$",//    
      letter_u:"^[A-Z]+$",//    
      mobile:"^0?(13|15|18|14)[0-9]{9}$",//  
      notempty:"^\\S+$",//  
      password:"^.*[A-Za-z0-9\\w_-]+.*$",//  
      fullNumber:"^[0-9]+$",//  
      picture:"(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$",//  
      qq:"^[1-9]*[1-9][0-9]*$",//QQ  
      rar:"(.*)\\.(rar|zip|7zip|tgz)$",//    
      tel:"^[0-9\-()()]{7,18}$",//       (        ,    ,   )
      url:"^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$",//url
      username:"^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$",//   
      deptname:"^[A-Za-z0-9_()()\\-\\u4e00-\\u9fa5]+$",//   
      zipcode:"^\\d{6}$",//  
      realname:"^[A-Za-z\\u4e00-\\u9fa5]+$",//     
      companyname:"^[A-Za-z0-9_()()\\-\\u4e00-\\u9fa5]+$",
      companyaddr:"^[A-Za-z0-9_()()\\#\\-\\u4e00-\\u9fa5]+$",
      companysite:"^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%=]*)?$"
  };
   
/**
 * id  html  ID,chName    , :  ,   
 */


注意:
先にjqueryを導入する必要があります