gpt4 book ai didi

javascript - Try and catch 对输入进行校验和验证,以检查 Javascript 中输入的三个部分

转载 作者:行者123 更新时间:2023-12-02 15:50:05 25 4
gpt4 key购买 nike

我的头从墙上弹起来,试图找出更好的方法来处理这个问题。我有一个很大的输入值,它有三个检查来检查字符串某些部分的总和以验证它。我现在在一个函数中使用三个 try/catch block 来运行检查,除了最终的验证检查似乎总是返回 true 之外,它似乎正在工作。我想知道的是 a)这是一个很好的使用方法,b)是否有一种更干净的方法可以使用 for 循环来执行此操作以及 c)为什么我的最终检查没有执行任何操作。任何帮助表示赞赏。如果有帮助的话,我可以访问 jQuery 和 Underscore.js,但我对 underscore 的工作不多。我在这里做了一个 fiddle :

Sample Fiddle

     window.onkeyup = keyup;
var number;

function keyup(e) {
number = e.target.value;
$('#numberValue').text(number);
// must be 10 characters long
if (number.length !== 30) {
return false;
}
number = "" + (number || "");
// run the checksum
var valid = false;
try {
var sum = (parseInt(number[0]) * 7) +
(parseInt(number[1]) * 3) +
(parseInt(number[2])) +
(parseInt(number[3]) * 7) +
(parseInt(number[4]) * 3) +
(parseInt(number[5])) +
(parseInt(number[6]) * 7) +
(parseInt(number[7]) * 3) +
(parseInt(number[8]));
alert(((sum % 10).toFixed(0)));
var checkDigit = ((sum % 10).toFixed(0));

if ((number[9]) === ("" + checkDigit)) {
alert('Our Checkdigit is valid', checkDigit);
valid = true;
}
} catch (e) {
alert('Fail for check 1!');
valid = false;
}
try {
var sum2 = (parseInt(number[13]) * 7) +
(parseInt(number[14]) * 3) +
(parseInt(number[15])) +
(parseInt(number[16]) * 7) +
(parseInt(number[17]) * 3) +
(parseInt(number[18]));
alert(((sum2 % 10).toFixed(0)));
var checkDigit2 = ((sum2 % 10).toFixed(0));

if ((number[19]) === ("" + checkDigit2)) {
alert('Our Checkdigit2 is valid', checkDigit2);
valid = true;
}
} catch (e) {
alert('Fail for check 2!');
valid = false;
}
try {
var sum3 = (parseInt(number[21]) * 7) +
(parseInt(number[22]) *3) +
(parseInt(number[23])) +
(parseInt(number[24]) * 7) +
(parseInt(number[25]) * 3) +
(parseInt(number[26]));
alert(((sum3 % 10).toFixed(0)));
var checkDigit3 = ((sum3 % 10).toFixed(0));

if ((number[27]) === ("" + checkDigit3)) {
alert('Our Checkdigit3 is valid',checkDigit3);
valid = true;
}
} catch (e) {
valid = false;
}

alert('All Good DUde!');
return valid;
}

最佳答案

这是执行此操作的方法。

我没有抛出任何错误,唯一的错误可能是数字不可解析,因此如果您愿意的话,如果您的总和检查可以验证应该足够好,您可以抛出它

window.onkeyup = keyup;
var number;

function keyup(e) {
number = e.target.value;
$('#numberValue').text(number);
// must be 10 characters long
if (number.length !== 30) {
return false;
}
number = "" + (number || "");
var valid = false;
//try{
var sum1 = returnSum(number,[0,1,2,3,4,5,6,7,8],[7,3,1,7,3,1,7,3,1]);
var sum2 = returnSum(number,[13,14,15,16,17,18],[7,3,1,7,3,1]);
var sum3 = returnSum(number,[21,22,23,24,25,26],[7,3,1,7,3,1]);
/*
//only if you are throwing err
}catch(e){
valid = false;
}
*/

if (number[9] === sum1 && number[19] === sum2 && number[27] === sum3) {
console.log(sum1 +'|' + sum2 + '|' + sum3);
valid = true;
}


console.log('All Good DUde!');
return valid;
}

function myParse(n){
return (isNaN(parseInt(n,10))) ? -1 : parseInt(n,10);
}

function returnSum(n,ind,mul){
var acc = 0;
var pNum = 0;
for(var i=0; i<ind.length; i++){
pNum = myParse(n[ind[i]]);
if(pNum == -1){
pNum=0;
//throw 'error';//if you really want to throw error on not a number / or your number should fail
}
acc += pNum * mul[i];
}
return (acc%10).toFixed(0)+'';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3> Sample test number to use -- copy and paste should work </p=h3>
<p>487013675311199070109160101300</p>

<input id="searchTxt" placeholder="add numbers together">
<div id='numberValue'>Number goes here</div>

干杯。喜悦

关于javascript - Try and catch 对输入进行校验和验证,以检查 Javascript 中输入的三个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com