-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (69 loc) · 2.41 KB
/
script.js
File metadata and controls
80 lines (69 loc) · 2.41 KB
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
document.getElementById('generateReal').addEventListener('click', generateRealNationalCode);
document.getElementById('generateRandom').addEventListener('click', generateRandomNationalCode);
document.getElementById('copyButton').addEventListener('click', copyNationalCode);
function generateRealNationalCode() {
let code = generateNationalCode(true);
document.getElementById('nationalCode').value = code;
}
function generateRandomNationalCode() {
let code = generateNationalCode(false);
document.getElementById('nationalCode').value = code;
}
function generateNationalCode(isReal) {
let code = '';
if (isReal) {
// Generate a real national code
code = generateRealCode();
} else {
// Generate a random national code
code = generateRandomCode();
}
return code;
}
function generateRealCode() {
let code = '';
let sum = 0;
for (let i = 0; i < 9; i++) {
let digit = Math.floor(Math.random() * 10);
code += digit;
sum += digit * (10 - i);
}
let remainder = sum % 11;
let controlDigit = remainder < 2 ? remainder : 11 - remainder;
code += controlDigit;
return code;
}
function generateRandomCode() {
let code = '';
for (let i = 0; i < 10; i++) {
code += Math.floor(Math.random() * 10);
}
return code;
}
function copyNationalCode() {
let nationalCode = document.getElementById('nationalCode');
nationalCode.select();
document.execCommand('copy');
showNotification('کد ملی کپی شد: ' + nationalCode.value);
}
function showNotification(message) {
// ایجاد عنصر اعلان
let notification = document.createElement('div');
notification.className = 'notification';
notification.textContent = message;
// اضافه کردن اعلان به بدنه صفحه
document.body.appendChild(notification);
// نمایش اعلان با انیمیشن
setTimeout(() => {
notification.classList.add('show');
}, 100);
// مخفی کردن اعلان پس از 3 ثانیه
setTimeout(() => {
notification.classList.remove('show');
notification.classList.add('hide');
// حذف اعلان از DOM پس از پایان انیمیشن
setTimeout(() => {
document.body.removeChild(notification);
}, 500);
}, 3000);
}