2011-06-21 47 views
1

我在验证脚本中遇到了一个问题,以检查澳大利亚邮政编码。 它似乎没有通过包含邮编值的多维数组递增。javascript验证多维数组中多个范围的值

这里的功能:

function validateAustralia(postcode, ranges) { 

      for (var i = 0; i < ranges.length; i++) { 
        console.log(i); 
//returns only 0, when it should return 0, 1, 2. 
        console.log("postcode: " + postcode + " " + "ranges: " + ranges); 
//returns postcode: 2000 ranges: 200,299,2600,2618,2900,2920 
        console.log("ranges - low: " + ranges[2][0] + " " + "ranges - high: " + ranges[2][1]); 
        //returns ranges - low: 2900 ranges - high: 2920 
       if (postcode >= ranges[i][0] && (postcode <= ranges[i][1])) { 
        valid = true; 
        //confirmation(); 
        //break; 
       } else { 
        inelegible(); 
        return false; 
       } 
      } 
    } 

对于新南威尔士州,例如

ranges = [ [1000, 2599], [2619, 2898], [2921, 2999] ]; 

它只有返回1000和2599 - 这是范围[0] [0]和范围[0 [1] 因此,有人输入Dubbo(位于新南威尔士州)的邮政编码被裁定为无效,因为其邮编-2830不在1000和2599之间。

jQuery's $ .each()正确地遍历第一个数组,但我不确定如何从第二个数组中获取值。

编辑: 好的,所以这是一个深夜,而我是盲目的。 小二郎的答案大部分都在下面,这里的一位朋友也指出了这一点:我在第一次运行后终止了迭代。 我移动了,如果else循环迭代,只是测试如果邮政编码是在范围内。如果是,它是有效的。 然后,如果有效=真我打电话确认功能和一切好:

function validateAustralia(postcode, ranges) { 
    for (var i = 0; i < ranges.length; i++) { 
      console.log(i); 
      // returns 0, 1, 2 ... 
      console.log("postcode: " + postcode + " " + "ranges: " + ranges); 
      // for Dubbo (2830), for example, returns postcode: 2830 ranges: 1000,2599,2619,2898,2921,2999 
      console.log("ranges - low: " + ranges[i][0] + " " + "ranges - high: " + ranges[i][1]); 
      // returns ranges - low: 1000 ranges - high: 2599, 
      //   ranges - low: 2619 ranges - high: 2898, ... 


     if (postcode >= ranges[i][0] && (postcode <= ranges[i][1])) { 
      valid = true; 
     // alert("valid =" + valid); 
     } 
     if (valid === true) { 
      confirmation(); 
      // all good 
     } else { 
      inelegible(); 
      // Sorry, mate 
     } 
    } 
} 

因为我是新来的,(长时间的倾听者,第一次调用者),我不能回答我的问题,但基本上就是这样。

这里的HTML和@nnnnnn和其他人谁希望看到调用函数: 用户从选择

<select id="states" name="states"> 
    <option selected="" value="">Please choose ...</option> 
     <optgroup label="Australia" id="australia"> 
     <option value="act">Australian Capital Territory </option> 
     <option value="nsw">New South Wales </option> 
     <!-- ...and so on for the rest of the states --> 

选择自己的状态并输入他们的邮政编码到一个文本框

<input id="postcode" name="postcode" type="text" maxlength="4" /> 

我得到正是如此

postcode = $('#postcode').val(); 

和检查抗拒着一系列邮政编码值

function checkAustralia(state, postcode, ranges) { 
    //  has to be in the range of values 
     switch (state) { 
      //Australian states 
      //match the whole postcode 
      //postcodes with a leading '0' are validated as whole numbers without the '0' 
     case 'act': 
      ranges = [ [200, 299], [2600, 2618], [2900, 2920] ]; 
      validateAustralia(postcode, ranges); 
      break; 
     case 'nsw': 
      ranges = [ [1000, 2599], [2619, 2898], [2921, 2999] ]; 
      validateAustralia(postcode, ranges); 
      break; 
// ...and so on for the rest of the states 
+0

如果您记录正确范围,会发生什么情况?这是你所期待的吗?如果你只能通过一次迭代,可能范围不是很好的形成? – thescientist

+1

1.您可以发布调用您的函数的代码吗?你已经给出了一个NSW范围的例子,但并不完全如此。 2.这种事情不应该被验证服务器端吗? – nnnnnn

+0

谢谢你们,我想我现在已经报道了。 @thescientist它是返回错误,停止迭代。 @nnnnnn 1.出于教育目的,我将发布调用函数2。我更喜欢它在服务器端完成,但这就是我们必须处理的:) –

回答

1

你的功能,只要第一个范围检查返回false。反向逻辑:return true如果值为,则为范围,但仅在循环完全耗尽时才返回false。

此外,您的代码并不总是显式返回一个值。这显然不是一个问题,但这可能与这里的混乱有关。

+0

感谢kojiro,这是大部分答案。 好的,所以这是一个深夜,我已经失明了:) –

+0

@David McK不客气。如果您满意,不要忘记标记答案正确。 – kojiro