0

我正在验证表格。它正确验证了所有字段,但是当我在文本字段中输入正确的数据时,它仍然不处理数据并返回everything wrong,而我希望它返回everything correct

这是我的js代码

var url, container, form,
    View = {
        init: function () {
            container     = $('.container');
            $(document).on('submit', '#form', View.findView);
        },

        loadViews: function (view, flag, e) {
            e.preventDefault();
            var search = $('.search');

            if( flag ) {
                if ( View.validation.emptyFieldsValidation() == true ) {
                    console.log('everything correct');
                } else {
                    console.log('everything incorrect');
                }
            }
            return false;
        },

        validation: {
            emptyFieldsValidation: function () {
                var inputs, field;

                $('#form input').each(function (i, el) {
                    inputs = $(this);
                    if ( inputs.val() == '' ) {
                        inputs.css('border', '1px solid red');
                        return false;
                    } else {
                        inputs.css('border', '1px solid #ccc');
                        if (inputs.hasClass('from')) {
                            console.log('from');
                            if (View.validation.validateAddress(inputs.val())) {
                                console.log('from correct address');
                                return true;
                            }
                            else {
                                inputs.css('border', '1px solid red');
                                console.log('from incorrect address');
                                //return false;
                            }
                        }
                        if (inputs.hasClass('to')) {
                            console.log('to');
                            if (View.validation.validateAddress(inputs.val())) {
                                console.log('to correct address');
                                //return true;
                            }
                            else {
                                inputs.css('border', '1px solid red');
                                console.log('to incorrect address');
                                //return false;
                            }
                        }
                        if (inputs.hasClass('time')) {
                            if (View.validation.validateForNumber(inputs.val())) {
                                console.log('time correct');
                                //return true;
                            }
                            else {
                                inputs.css('border', '1px solid red');
                                console.log('time incorrect');
                                //return false;
                            }
                        }
                    }
                });
                return false;
            },

            validateAddress: function (val) {
                var streetregex = /^[\w+\s]+\d+,\s*[\s\w]+$/;
                if (streetregex.test(val)) return true;
                else return false;
            },

            validateForNumber: function (val) {
                if (!isNaN(val)) return true;
                else return false;
            }
        }
    };

View.init();

​这是我的html代码

<section class="search">
<form id="form" action="">
    <section>
        <input type="text" name="from" class="from address" id="addressone" placeholder="From">
    </section>
    <section>
        <input type="text" name="to" class="to address" id="addresstwo" placeholder="To">
    </section>
    <section>
        <label>Time</label>
        <input type="text" name="hrs" placeholder="Hours" class="t1 time">
        <span>:</span>
        <input type="text" name="mins" placeholder="Mins" class="t1 time">
        <button class="button">Search</button>
    </section>
</form>
</section>

更新

我在控制台中得到这个输出

截屏

4

1 回答 1

1

您似乎错过了您的emptyFieldsValidation函数将 always的事实return false,因为其主体中的所有其他return语句实际上都是“迭代器”函数的一部分。说到后者,注意return false$.each回调中会停止迭代。

一种可能的解决方法是将迭代器函数中的所有这些return false语句替换为填充验证错误数组。然后($.each在行之后)你可以检查它的长度(或者只是做return !!errors.length)。例如:

emptyFieldsValidation: function () {
  var errors = [];
  ...
  $('#form input').each(function() {
    if (! someValidationCheck) { 
      errors.push(someValidationAttribute);  
    }
  });
  ...
  return !!errors.length;
于 2012-11-05T12:07:08.457 回答