You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.5 KiB

  1. 
  2. //----------- [輸入控制,僅限輸入 0~9 十個字元]-----------
  3. //eg:onkeypress="return inputNubmerFloat()"
  4. function inputNubmerFloat() {
  5. var key = window.event.keyCode;
  6. if ((key >= 48 && key <= 57)) {
  7. return true;
  8. }
  9. else {
  10. window.event.keyCode = 0;
  11. return true;
  12. }
  13. }
  14. //----------- [輸入控制,僅限輸入 0~9 十個字元和一個小數點]-----------
  15. //eg:onkeypress="return inputNubmerFloat()"
  16. function KeyPress(objTR) {
  17. var txtval = objTR.value;
  18. var key = event.keyCode;
  19. if ((key < 48 || key > 57) && key != 46) {
  20. event.keyCode = 0;
  21. }
  22. else {
  23. if (key == 46) {
  24. if (txtval.indexOf(".") != -1 || txtval.length == 0)
  25. event.keyCode = 0;
  26. }
  27. }
  28. }
  29. //----------- [輸入控制,僅限輸入字母] -----------------
  30. //eg:onkeypress="return inputLetter()"
  31. function inputLetter() {
  32. var key = window.event.keyCode;
  33. if ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) {
  34. return true;
  35. }
  36. else {
  37. window.event.keyCode = 0;
  38. return true;
  39. }
  40. }
  41. //--------- [移除非數字內容] -----------
  42. //eg:onblur="isMubmer(id)";
  43. function isMubmer(id) {
  44. var textbox = document.getElementById(id);
  45. var textvalue = textbox.value;
  46. if (textvalue != "") {
  47. textbox.value = textvalue.replace(/[^0-9 .]/g, '');
  48. SetHidStats(id, (textbox.value.length == textvalue.length));
  49. textvalue = trim(textbox.value, ","); //先去千分位
  50. textvalue = textvalue * 1;
  51. textbox.value = addComma(textvalue.toString()); //再加千分位
  52. }
  53. else {
  54. textbox.value = "0";
  55. }
  56. }
  57. //--------- [移除非字母內容] -----------
  58. //eg:onblur="isLetter(id)";
  59. function isLetter(id) {
  60. var textbox = document.getElementById(id);
  61. var textvalue = textbox.value;
  62. textbox.value = textbox.value.replace(/[^a-z A-Z]/g, '')
  63. SetHidStats(id, (textbox.value.length == textvalue.length));
  64. }
  65. //--------- [判斷EMail格式(驗證控件)]---------
  66. //ge:onblur="return checkEmail(id)";
  67. function checkEmail(id) {
  68. var textbox = document.getElementById(id);
  69. var strText = trimSpace(textbox.value);
  70. var objRe = /^[\w]+@([\w]+\.)+[\w]{2,3}$/;
  71. if (strText == "") return true;
  72. SetHidStats(id, objRe.test(strText));
  73. if (objRe.test(strText)) {
  74. return true;
  75. }
  76. else {
  77. return false;
  78. }
  79. }
  80. //---------------- [自定義方法] ----------------------------
  81. //去頭尾空格
  82. function trimSpace(str) {
  83. var instring = str.toString();
  84. var value = new String();
  85. //去頭
  86. for (var i = 0; i < instring.length; i++) {
  87. if (instring.charAt(i) != " ") {
  88. value = instring.substring(i);
  89. break;
  90. }
  91. }
  92. //去尾
  93. while (value.charAt(value.length - 1) == " ") {
  94. value = value.substring(0, value.length - 1);
  95. }
  96. return value;
  97. }
  98. //記錄檢測狀態
  99. function SetHidStats(id, bool) {
  100. var Stats = document.getElementById(id.replace(/AmtTextBox/g, "AmtHidStats"));
  101. if (id.replace(/AmtTextBox/g, "AmtHidStats").indexOf("_AmtHidStats") == -1) {
  102. Stats = document.getElementById(id.replace(/AmtTextBox/g, "AmtHidStats") + "_AmtHidStats");
  103. }
  104. if (bool) {
  105. Stats.value = "Y";
  106. }
  107. else {
  108. Stats.value = "N";
  109. }
  110. }
  111. function addComma(objvalue) {
  112. var frontStr = "";
  113. var backStr = "";
  114. var sect = ""
  115. if (objvalue.indexOf(".") != -1) {
  116. frontStr = objvalue.substring(0, objvalue.indexOf("."));
  117. backStr = objvalue.substring(objvalue.indexOf(".") + 1);
  118. }
  119. else { //ming941214負數的處理
  120. if (objvalue.indexOf("-") != -1) {
  121. sect = "-";
  122. frontStr = objvalue.substr(1)
  123. } else {
  124. frontStr = objvalue;
  125. }
  126. //frontStr=objvalue;
  127. backStr = "";
  128. }
  129. var leftCharsCount = frontStr.length % 3;
  130. //共被","分成幾段
  131. var totalFieldsCount = (frontStr.length - leftCharsCount) / 3;
  132. var i = 0;
  133. var newFrontStr = "";
  134. newFrontStr += frontStr.substr(0, leftCharsCount);
  135. if (newFrontStr.length != 0) {
  136. newFrontStr += ",";
  137. }
  138. for (i = 0; i < totalFieldsCount; i++) {
  139. newFrontStr += frontStr.substr(i * 3 + leftCharsCount, 3) + ",";
  140. }
  141. newFrontStr = newFrontStr.substr(0, newFrontStr.length - 1);
  142. if (objvalue.indexOf(".") >= 0) {
  143. objvalue = newFrontStr + "." + backStr
  144. }
  145. else {
  146. objvalue = newFrontStr
  147. }
  148. objvalue = sect + objvalue;
  149. return objvalue;
  150. }
  151. function trim(str, Char) {
  152. if (Char == null) {
  153. Char = " ";
  154. }
  155. var resultStr = str;
  156. while (resultStr.indexOf(Char) >= 0) {
  157. resultStr = resultStr.replace(Char, '');
  158. }
  159. return resultStr;
  160. }
  161. function clearComma(id) {
  162. var textbox = document.getElementById(id);
  163. if (textbox.value.indexOf(",") != -1) {
  164. textbox.value = trim(textbox.value, ",");
  165. }
  166. if (textbox.value == "0") {
  167. textbox.value = "";
  168. }
  169. // moveEnd(textbox);
  170. // var txt = textbox.createTextRange();
  171. // txt.moveStart('character', textbox.value.length);
  172. // txt.collapse(true);
  173. // txt.select();
  174. }
  175. function moveEnd(obj) {
  176. //obj.focus();
  177. var len = obj.value.length;
  178. if (document.selection) {
  179. var sel = obj.createTextRange();
  180. sel.moveStart('character', len);
  181. sel.collapse();
  182. sel.select();
  183. } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
  184. obj.selectionStart = obj.selectionEnd = len;
  185. }
  186. }