commit_validate.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*判断输入是否为正确的身份证*/
  2. function isIdcard(v)
  3. {
  4. if(!(/^\d{17}(\d|x)$/i.test(v) || /^\d{15}$/i.test(v))) return false;
  5. var provinceCode = parseInt(v.substr(0,2));
  6. if((provinceCode < 11) || (provinceCode > 91)) return false;
  7. var forTestDate = v.length == 18 ? v : v.substr(0,6)+"19"+v.substr(6,15);
  8. var birthday = forTestDate.substr(6,8);
  9. if(!isDate(birthday,'yyyyMMdd')) return false;
  10. if(v.length == 18) {
  11. v = v.replace(/x$/i,"a");
  12. var verifyCode = 0;
  13. for(var i = 17;i >= 0;i--)
  14. verifyCode += (Math.pow(2,i) % 11) * parseInt(v.charAt(17 - i),11);
  15. if(verifyCode % 11 != 1) return false;
  16. }
  17. return true;
  18. }
  19. function isDate(v,dateFormat){
  20. var MONTH = "MM";
  21. var DAY = "dd";
  22. var YEAR = "yyyy";
  23. var regex = '^'+ dateFormat.replace(YEAR,'\\d{4}').replace(MONTH,'\\d{2}').replace(DAY,'\\d{2}')+'$';
  24. if(!new RegExp(regex).test(v)) return false;
  25. var year = v.substr(dateFormat.indexOf(YEAR),4);
  26. var month = v.substr(dateFormat.indexOf(MONTH),2);
  27. var day = v.substr(dateFormat.indexOf(DAY),2);
  28. var d = new Date(format('%s/%s/%s',[year,month,day]));
  29. return ( parseInt(month, 10) == (1+d.getMonth()) ) &&
  30. (parseInt(day, 10) == d.getDate()) &&
  31. (parseInt(year, 10) == d.getFullYear() );
  32. }
  33. function format(str,args) {
  34. args = args || [];
  35. var result = str;
  36. for (var i = 0; i < args.length; i++){
  37. result = result.replace(/%s/, args[i]);
  38. }
  39. return result;
  40. }
  41. /*判断输入是否为合法的手机号码*/
  42. function isphone(inputString)
  43. {
  44. var partten = /^0?(13[0-9]|15[0-9]|18[0-9]|14[57]|17[0-9]|16[0-9]|19[0-9])[0-9]{8}$/;
  45. if(partten.test(inputString))
  46. {
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. /*判断输入是否为合法的医社保卡号*/
  55. function isSsc(inputString)
  56. {
  57. var partten = /^[a-zA-Z0-9]+$/;
  58. if(partten.test(inputString))
  59. {
  60. return true;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. //处理表情
  68. function utf16toEntities(str) {
  69. var patt=/[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
  70. str = str.replace(patt, function(char){
  71. var H, L, code; if (char.length===2) {
  72. H = char.charCodeAt(0); // 取出高位
  73. L = char.charCodeAt(1); // 取出低位
  74. code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
  75. return "&#" + code + ";";
  76. } else { return char; } });
  77. return str;
  78. }