通过循环和数组减少If语句(Reduce If statement by looping and Arrays)
我试图缩短一些if语句
以前我有以下内容:
var regex = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?()]/); var ltr = /[a-zA-Z ]+/; var mistakeA; var mistakeB; var mistakeC; var input = document.myform.myinputname.value; var input2 = document.myform.myinputname.value; if(ltr.test(input) && !(regex.test(input))){ myinputid.style.border ='1.5px solid red; mistakeA; } else if (regex.test(input) && !(ltr.test(input))){ myinputid.style.border ='1.5px solid red; mistakeB; } // same for input 2所以基本上是检查输入是否正确。 到目前为止,所有工作都符合逻辑。 但是我有太多的If-Statement所以现在我试图将它们缩短如下:
var input [input1,input2,input3,input4]; for (i= 0; i < input.length; i++){ if (ltr.test(input[]) &&!(regex.test(input[]))){ myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }那么我做错了什么? 或者我如何缩短我的If-Statements? 我有8个结构相同的陈述。 不调用数组(作为输入)。 它不起作用,因为.test(input [])必须在某种程度上是错误的。
I'm trying to shorten a couple of if-statement
Previously I had following:
var regex = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?()]/); var ltr = /[a-zA-Z ]+/; var mistakeA; var mistakeB; var mistakeC; var input = document.myform.myinputname.value; var input2 = document.myform.myinputname.value; if(ltr.test(input) && !(regex.test(input))){ myinputid.style.border ='1.5px solid red; mistakeA; } else if (regex.test(input) && !(ltr.test(input))){ myinputid.style.border ='1.5px solid red; mistakeB; } // same for input 2So basically it's a check if the input is correct. And everything worked so far logicalwise. But i had too many If-Statement so now i tried to shorten them as following:
var input [input1,input2,input3,input4]; for (i= 0; i < input.length; i++){ if (ltr.test(input[]) &&!(regex.test(input[]))){ myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }So what am i doing wrong? Or how can i shorten my If-Statements? I have 8 Statements with the same structure. The array (which are the inputs ) aren't called. it's not working because the .test(input[]) must be somehow wrong.
最满意答案
var input = []; // that's how you declare an array input.push(document.myform.myinputname.value); // add a new item input.push(document.myform.myinputname.value); // add another // ... // Or: (notice =) //var input = [document.myform.myinputname.value, document.myform.myinputname.value, ...]; // just an advice (use var i instead of i) to not make i global for (var i = 0; i < input.length; i++){ if (ltr.test(input[i]) && !(regex.test(input[i]))){ // use subscripts to indicate which item of the array to test myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } } var input = []; // that's how you declare an array input.push(document.myform.myinputname.value); // add a new item input.push(document.myform.myinputname.value); // add another // ... // Or: (notice =) //var input = [document.myform.myinputname.value, document.myform.myinputname.value, ...]; // just an advice (use var i instead of i) to not make i global for (var i = 0; i < input.length; i++){ if (ltr.test(input[i]) && !(regex.test(input[i]))){ // use subscripts to indicate which item of the array to test myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }更多推荐
发布评论