-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput-handler.js
84 lines (56 loc) · 1.86 KB
/
input-handler.js
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
$('#buttonStart').click(function () {
let openText = $('#openText').val();
let key = $('#key').val();
let cipherText = $('#cipherText').val();
if (openText.length > 0)
openToCrypt(key, openText, languageTest(openText, key));
else
cryptToOpen(key, cipherText, languageTest(cipherText, key));
});
$('input[type=radio]').change(function () {
clearAllinput();
});
function clearAllinput() {
$('#openText').val("");
$('#key').val("");
$('#cipherText').val("");
}
$('input').keyup(function () {
$(this).val(clearInputByPattern($(this).val()));
});
function clearInputByPattern(text) {
text = text.replace(/[0-9]/g, '');
if ($('#ruRadio').prop("checked") === true) {
text = text.replace(/[A-Za-z]/g, '');
} else {
text = text.replace(/[А-Яа-я]/g, '');
}
return text;
}
function languageTest(text) {
if ((text.search(/[а-яА-Я]+/gu) !== -1 && text.search(/[a-zA-Z]+/gu) !== -1)) {
alert('Некорректный ввод');
clearAllinput();
throw new Error('Некорректный ввод. Встретились два типа языка');
}
if (text.search(/[a-zA-Z]+/gu) !== -1)
return 'en';
else
return 'ru';
}
function openToCrypt(key, text, lang) {
if (!(key.length > 0 && text.length > 0)) {
alert('Не все поля заполнены');
throw new Error('empty fields');
}
clearAllinput();
$('#cipherText').val(vizhener.encryption(lang, text.toUpperCase(), key.toUpperCase()));
}
function cryptToOpen(key, text, lang) {
if (!(key.length > 0 && text.length > 0)){
alert('Не все поля заполнены');
throw new Error('empty fields');
}
clearAllinput();
$('#openText').val(vizhener.decryption(lang, key.toUpperCase(), text.toUpperCase()));
}