使用JS从预定义的字符列表生成或验证校验和字符串



我正在尝试创建一组JavaScript函数,这些函数可以从一些定义的字母数字字符串生成一个6个字符的代码(可以更改,因此对拉丁语或西里尔文等字母用户来说更容易,具体取决于字母用户(,并且如果代码是键入的而不是生成的,还可以验证。我试图使用这个线程中的代码和我正在使用的应用程序中已经存在的文本框的组合,但它根本不起作用,因为我对JS了解不够。

以下是我开始构建自己的东西,正如你所看到的,我在评论中从头到尾概述了我对这个过程的想法。不过,我不知道如何为每个字母指定一个特定的值。

这里的想法是,我需要它的唯一性,而不是安全性。速度也更可取,但这些似乎都不太复杂,以至于无法快速运行。当设备(Android平板电脑(运行此代码时,它们上没有网络连接。

// Writing Psudo Code for a JS Function to create (or validate) a checksum ID variable from a list of input characters:
// Setup:
// Take in a list of possible alpha characters:
var charset = 'АБВГДЖЗКЛМНОПРСТУФХЧЭЮЯ';

// Make sure all are letters, no numbers or symbols. Throw error.
// /^[a-zA-Z]+$/ or maybe /[wа-яА-Я]+/ig
// Now calculate the length (num of characters), n:
function NumberOfValidInputCharacters () { 
return charset.length;
}
// Create a string of 5 random characters from the list.
function makeid() {
var text = '';
// Note:change 4 to the desired length of the ID. Since it starts at 0, 4 means an actual length of 5.
for (var i = 0; i <= 4; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}

// Assign each character a value from 1 to n: value1 - value5
//**I think in order to do this I'll have to set the return to an array, as JS doesn't allow multiple returns.


// Math Ops:
function makechecksum() {
// Separate out the characters into 5 separate vars: char1 - char5

// Map to values from setup.

// Run some math ops on the 5 values as follows:
// 2*value1
var val1 = 2*value1
// value2*value2
var val2 = value2**2
// (value3 +1)*value4
var val3 = (value3 +1)*value4
// (value5^3) - 1
var val4 = (value5**3) - 1
// Place the results into an array
var ArrayValue = [val1, val2, val3, val4]
// Sum the results
var addend = sum(ArrayValue)

// Divide the result by charset.length, capture the remainder.
var final_digit = (addend % charset.length);

// Assign the remainder to it's corresponding character from previous step, label as char6.


// Create id code by concatenating char1 - char6. Return.
var full_id = text.concat(final_digit);
}
//Validate ID code (Take a code entered and validate it.)
// Duplicate variable, remove char6, 
testString = testString.toUpperCase();
var testString = text.substring(0,4)
// Now run the rest of string of new vars characters through value assignment and Math Ops.

// Compare origial variable to newly created one. If unequal throw error.

//Stuff newly generated Checksum ID into the blank
inputs.stu_id.addEventListener('change', function() { 
if (inputs.student_id.value == '') 
inputs.student_id.value = codePoint()
})

我已经尝试了算法的第一部分,但我不确定makeID()的结果是字符串还是数字数组。我带着绳子去了。使用为字符指定一个数值https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt和https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode相反。问题是,您的数学运算产生的大值不符合String.fromCharCode()0 - 65535范围。结果,您将看到";问号";unicode符号。

const charset = 'АБВГДЖЗКЛМНОПРСТУФХЧЭЮЯ';
const numSampledChars = 5;
const makeID = (str) => {
const charCodes = Array.from(str.substr(0, numSampledChars)).map(char => char.charCodeAt(0));
console.log('charCodes:', charCodes);
charCodes[0] = charCodes[0] * 2;
charCodes[1] = charCodes[1] * charCodes[1];
charCodes[2] = (charCodes[2] + 1) * charCodes[3];
charCodes[3] = (charCodes[4] ** 3) - 1;
console.log('charCodes after math:', charCodes);
const sumCharCodes = charCodes.reduce((result, code) => result + code, 0);
console.log('sumCharCodes:', sumCharCodes);
const charCodeToAppend = sumCharCodes % charset.length;
console.log('charCodeToAppend:', charCodeToAppend);
return charCodes.concat([charCodeToAppend]).map(code => String.fromCharCode(code)).join('');
};
const inputStr = 'МНОПРСТУФМНОПРСТУФМНОПРСТУФ';
console.log('makeID result:', makeID(inputStr));

此打印:

charCodes: [ 1052, 1053, 1054, 1055, 1056 ]
charCodes after math: [ 2104, 1108809, 1113025, 1177583615, 1056 ]
sumCharCodes: 1179808609
charCodeToAppend: 11
makeID result: ࠸﯁翿Р

最新更新