具体逻辑

  1. 3段代码加1个随机数,3段代码不限制长度
  2. 若3段代码中包含英文字母,则最后的随机数以大写字母结尾
  3. 若3段代码是纯数字,则最后的随机数以数字结尾

实现

由于想到啥写啥,所以逻辑其实很垃圾且为了方便全部集成在一个HTML文件下了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>生成码</title>
</head>
<script type="text/javascript">

//按下回车时,相当于按下生成按钮
document.onkeydown=keyListener;
function keyListener(e){
// 当按下回车键,执行我们的代码
if(e.keyCode == 13){
generate();
}
}

// 复制字符串到剪切板
function copyToClipboard(elem) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
// can just use the original source element for the selection and copy
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);

// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch (e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}

if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
target.textContent = "";
}
return succeed;
}

// 复制全部按钮事件
function copy() {
copyToClipboard(document.getElementById("showText"));
alert("复制成功!");
}

// 生成按钮事件
function generate() {
document.getElementById("showText").value = "";
var s1 = document.getElementById("text1").value;
var s2 = document.getElementById("text2").value;
var s3 = document.getElementById("text3").value;
var textbox = document.getElementById("showText");
var limitDigit = new RegExp("^\\+?[1-9][0-9]*$");//正则
if (limitDigit.test(s1) && limitDigit.test(s2) && limitDigit.test(s3)) {
s3 = parseInt(document.getElementById("text3").value);
for (var i = 0; i < parseInt(document.getElementById("text4").value); i++) {
var s4 = randomDigit(s1, s2, s3);
textbox.value = textbox.value + (s1 + s2 + s3 + s4 + '\n');
s3++;
}
} else {
var chars = "0123456789ABCDEFGHIZKLMNOPQRSTUVWXYZ";
for (var i = 0; i < parseInt(document.getElementById("text4").value); i++) {
var s4 = chars.charAt(parseInt(Math.random() * 36));
textbox.value = textbox.value + (s1.toUpperCase() + s2.toUpperCase() + s3.toUpperCase() + s4 + '\n');
var s3_10 = parseInt(s3, 36);
s3 = (++s3_10).toString(36);
}
}
}

//重置按钮事件
function reset(){
document.getElementById("text1").value = "";
document.getElementById("text2").value = "";
document.getElementById("text3").value = "";
document.getElementById("text4").value = "";
document.getElementById("showText").value = "";
}

//纯数字时的尾数
function randomDigit(s_1, s_2, s3) {
var pair = new Array();
var single = new Array();
var randomDigit = 0;
var n1 = s_1.length;
var n2 = s_2.length;
var n3 = (s3 + "").length;
var sumSingle = 0;
var sumPair = 0;
var sum = 0;
var s1 = parseInt(s_1);
var s2 = parseInt(s_2);
for (var j = 1; j <= n3; j++) {
if (j % 2 != 0) {
single.push(s3 % 10);
s3 = parseInt(s3 / 10);
} else {
pair.push(s3 % 10);
s3 = parseInt(s3 / 10);
}
}
if (n3 % 2 != 0 && n2 % 2 != 0) {
for (var j = 1; j <= n2; j++) {
if (j % 2 != 0) {
pair.push(s2 % 10);
s2 = parseInt(s2 / 10);
} else {
single.push(s2 % 10);
s2 = parseInt(s2 / 10);
}
}
for (var j = 1; j <= n1; j++) {
if (j % 2 != 0) {
single.push(s1 % 10);
s1 = parseInt(s1 / 10);
} else {
pair.push(s1 % 10);
s1 = parseInt(s1 / 10);
}
}
} else if (n3 % 2 != 0 && n2 % 2 == 0) {
for (var j = 1; j <= n2; j++) {
if (j % 2 != 0) {
pair.push(s2 % 10);
s2 = parseInt(s2 / 10);
} else {
single.push(s2 % 10);
s2 = parseInt(s2 / 10);
}
}
for (var j = 1; j <= n1; j++) {
if (j % 2 != 0) {
pair.push(s1 % 10);
s1 = parseInt(s1 / 10);
} else {
single.push(s1 % 10);
s1 = parseInt(s1 / 10);
}
}
} else if (n3 % 2 == 0 && n2 % 2 != 0) {
for (var j = 1; j <= n2; j++) {
if (j % 2 != 0) {
single.push(s2 % 10);
s2 = parseInt(s2 / 10);
} else {
pair.push(s2 % 10);
s2 = parseInt(s2 / 10);
}
}
for (var j = 1; j <= n1; j++) {
if (j % 2 != 0) {
pair.push(s1 % 10);
s1 = parseInt(s1 / 10);
} else {
single.push(s1 % 10);
s1 = parseInt(s1 / 10);
}
}
} else {
for (var j = 1; j <= n2; j++) {
if (j % 2 != 0) {
single.push(s2 % 10);
s2 = parseInt(s2 / 10);
} else {
pair.push(s2 % 10);
s2 = parseInt(s2 / 10);
}
}
for (var j = 1; j <= n1; j++) {
if (j % 2 != 0) {
single.push(s1 % 10);
s1 = parseInt(s1 / 10);
} else {
pair.push(s1 % 10);
s1 = parseInt(s1 / 10);
}
}
}
for (var j = 0; j < single.length; j++) {
sumSingle += single[j];
}
for (var j = 0; j < pair.length; j++) {
sumPair += pair[j];
}
sum = sumSingle * 3 + sumPair;
if (sum % 10 == 0) {
randomDigit = 0;
} else {
randomDigit = 10 - (sum % 10);
}
return randomDigit;
}
</script>
<body>
<fieldset>
<legend align="center"><h2>生成码</h2></legend>
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>&nbsp;<label for="text1">国家代码:</label></td>
<td>
<!-- 只允许输入数字和字母 -->
<input type="text" name="text1" id="text1" onkeyup="value=value.replace(/[\W]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" />
</td>
<td>&nbsp;<label for="text2">厂商代码:</label></td>
<td>
<!-- 只允许输入数字和字母 -->
<input type="text" name="text2" id="text2" onkeyup="value=value.replace(/[\W]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" />
</td>
<td>&nbsp;<label for="text3">商品代码:</label></td>
<td>
<!-- 只允许输入数字和字母 -->
<input type="text" name="text3" id="text3" onkeyup="value=value.replace(/[\W]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" />
</td>
<td>&nbsp;<label for="text4">生成数量:</label></td>
<td>
<!-- 只允许输入数字 -->
<input name="text4" id="text4" type="text" onkeyup="value=this.value.replace(/\D+/g,'')" />
</td>
</tr>
</table>
<div align="center">
<br>
<textarea rows="23" cols="102" id="showText" style="font-size: 18px; font-weight: bold;"></textarea>
<br><br>
<!-- 事件函数是generate() -->
<button type="button" id="generate" onclick="generate();">生成</button>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!-- 事件函数是reset() -->
<button type="button" onclick="reset();">重置</button>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!-- 事件函数是copy() -->
<button type="button" onclick="copy();">复制全部</button>
</div>
</fieldset>
</body>
</html>