我正在验证表格。它正确验证了所有字段,但是当我在文本字段中输入正确的数据时,它仍然不处理数据并返回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>
更新
我在控制台中得到这个输出