rails html5,Rails with HTML5 validation and JS

In my rails app I have validation form for matched email. For some reason validation doesn't worked - it shows error below form (when email addresses are not equal) but it submits the form and goes to the next page anyway.

validations.js

if (registrationsForm.length > 0) {

restrictZipCodeToNum();

restrictPhoneToNum();

submit.on('click', function(e) {

var invalidInput = $('input:invalid');

e.preventDefault();

if (emailField.val() !== emailConfirmationField.val() && emailField.length > 0) {

emailInvalidMsg.show();

emailField.addClass("invalid");

emailConfirmationField.addClass("invalid");

if (emailField.val() !== '') obligatoryInvalidMsg.hide();

}

validateEmail();

scrollToFirstInvalid();

if (invalidInput.length === 0 && !fileInput.hasClass('invalid')) {

form.submit();

} else {

invalidInput.addClass('invalid');

validateInput();

}

});

}

new.html.erb

class="col-sm-12 col-md-12 col-xs-12 text-center bank-employees-users-registration__registrations-submit--wrapper">

Any idea? I put the debugger below the submit.on('click', function(e) { to check what is under invalidInput but it shows correct value - input:invalid. So maybe some form is overriding the whole logic, I don't know, any suggestions are welcomed.

Function of checking if email address are the same is here:

function validateEmail() {

$('input[type="email"]').change(function() {

if (emailField.val() === emailConfirmationField.val() && emailField.val() !== '') {

obligatoryInvalidMsg.hide();

emailInvalidMsg.hide();

emailField.removeClass("invalid");

emailConfirmationField.removeClass("invalid");

} else if (emailField.val() === emailConfirmationField.val() && emailField.val() === '') {

emailInvalidMsg.hide();

obligatoryInvalidMsg.show();

emailField.addClass("invalid");

emailConfirmationField.addClass("invalid");

} else {

obligatoryInvalidMsg.hide();

emailInvalidMsg.show();

emailField.addClass("invalid");

emailConfirmationField.addClass("invalid");

}

});

}

EDIT

After debugging I think the main problem is in last if block -> if (invalidInput.length === 0 .... When I put debugger below this block and in console wrote invalidInput.length I will get 0 in case when first address in from is 123@example.com and the confirmation email address is 4321@example.com. When I have empty field it shows correctly as 1 or 2 (depends on empty field). No idea what's going wrong.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值