Jquery自定义验证器不适用于澳大利亚商业号码 - 带掩码的ABN

时间:2016-06-01 12:42:17

标签: javascript jquery validation

我一直在研究这个问题已经有一段时间了,最​​终诉诸于jsfiddle,这会导致我无法修复的错误。它给出了这个错误:

  

{"错误":"外壳形式无法验证{' html_initial_name':

除了jsFiddle问题之外,验证器的问题是该方法被设置为运行纯数而没有破折号。

所以我添加了一个新的变量,它删除了破折号

var testValue = parseInt(value.replace(/-/g, ""));

但是它给出了错误或NaN - 不是最终Tally变量的数字。

我想要做的是以掩码格式应用真正的ABN号码,例如81-050-237-986,并使用该方法处理它并获得真实。

这是验证器功能:

      jQuery.validator.addMethod('abnValidate', function abnValidate(value, element) {
        if (value.length == 14) {
            var testValue = parseInt(value.replace(/-/g, ""));
            var weighting = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
            var tally = (parseInt(testValue[0]) - 1) * weighting[0];
            for (var i = 1; i < testValue.length; i++) {
                tally += (parseInt(value[i]) * weighting[i]);
            }
            return (tally % 89) == 0;
        }
        else {
            return true;
        }
    });

    $("#ABN").rules("add", {
        abnValidate: true,
        messages: {
            abnValidate: "The ABN does not conform to the ATO's algorithm"
        }
    });

修改的 您可以查看ATO使用的公式here

基本上这个过程如下:

To verify an ABN:
To verify an ABN:
1. Subtract 1 from the first (left) digit to give a new eleven digit number
2. Multiply each of the digits in this new number by its weighting factor
3. Sum the resulting 11 products
4. Divide the total by 89, noting the remainder
5. If the remainder is
zero
the number is valid

1 个答案:

答案 0 :(得分:0)

完整代码   abn number validation source code with alogo information

****我的ABN号码验证(带有掩盖GooD运气的ABN号码格式)

 //html part

    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!------ Include the above in your HEAD tag ---------->
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
    <div class="container">
     <div class="row">



             <div>
       <form  id="register_form" class="forms-sample" method="post" action="register" enctype="multipart/form-data">
       <div class="form-group">
                              <label >ABN Number</label>
                               <input id="contact_abn_no" type="text" name="abn_no"  class="form-control" value="" placeholder="ABN Number">


                               <span class="text-danger" style="font-size:12px;display:none;">{{ $errors->first('abn_no') }}</span>

                </div>
                <br/>
                            <div>
                            <button style="margin-top:0px;" type="submit" class="btn btn-primary btn-lg btn-block btn-block_top_margin btnsubload">Submit</button>
                            </div>
               </form>  
               </div>
          </div>
    </div>
    //jquery part
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

        <script type="text/javascript">

    $.ajaxSetup({

        headers: {

            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

        }

    });

    $(function() {
                $("#contact_abn_no").mask("00 000 000 000", {
        onChange: function () {
            console.log($('#contact_abn_no').val());
        }
    });
            });




    $(function(){
        $("#register_form").validate({ 
        rules: {


            "abn_no":  { 
                checkAbn : true,
                required: true

            }



        }, 
        messages: {

            "abn_no": {
            required: "Please Enter ABN Number" 
            }

        },
        highlight: function (element, errorClass, validClass) { 

       $("#loading").css("visibility", "hidden"); 
        return false;
        $("#loading").css("visibility", "visible");
        },
        errorElement: 'span',
        submitHandler: function (form) {
        form.submit();
        }
        });



    $.validator.addMethod("checkAbn", function(value, element) {


        var abn_val = $("#contact_abn_no").val()
        if (abn_val.trim() != '') {
                        var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
                        if (abn_val.length == 14) {
                            //remove spaces from string
                            abn_val = abn_val.replace(/\s+/g, '');
                            // strip anything other than digits
                            abn_val = abn_val.replace("/[^\d]/", "");
                            // check length is 11 digits
                            if (abn_val.length == 11) {
                                // apply ato check method 
                                var sum = 0;
                                for (var i = 0; i < 11; i++) {
                                    var digit = abn_val[i] - (i ? 0 : 1);
                                    var sum = sum + (weights[i] * digit);
                                }
                                return ((sum % 89) == 0);
                            } else {
                                return false;
                            }
                        }
                    } else {
                        return true;
                    }

      // return true;
    }, "ABN number not vailid.");

    })


    </script>


  [1]: http://www.expertsuggestion.com/2019/06/abn-number-validation-with-javascript.html