简体   繁体   English

如何实时比较2个表单输入

[英]how to compare 2 form inputs live

I'm trying to compare two form inputs "password" and re-enter-password" to make sure there the same. I validate the password by sending it to a separate PHP that echoes back the results(which works fine) 我正在尝试比较两个表单输入“ password”和“ re-enter-password”,以确保它们相同。我通过将密码发送到一个单独的PHP来验证密码,以回显结果(工作正常)

<script type="text/javascript">
  $(document).ready(function() {
    $('#password_feedback').load('password-check.php').show();

    $('#password_input').keyup(function() {

      $.post('password-check.php', {
          password: form.password.value
        },
        function(result) {
          $('#password_feedback').html(result).show();
        });

    });
  });
</script>

I tried sending password and re-enter=password to a PHP to compare with no luck. 我尝试发送密码并重新输入=密码到PHP以比较没有运气。 Can I compare the two with every keyup. 我可以将每个keyup两者进行比较。

Not necessary jQuery, add the function: 不需要jQuery,添加功能:

function checkPass(input) {
    if (input.value != document.getElementById('re-enter-password').value) {
        input.setCustomValidity('Passwords should match.');
    } else {
        input.setCustomValidity('');
    }
}

Add this to your re-enter-password: oninput="checkPass(this)" 将此添加到您的重新输入密码中: oninput="checkPass(this)"

OR just call this function in the part where you want to make the comparison: 或者只是在您要进行比较的部分中调用此函数:

function checkPass() {
    var input = document.getElementById('password');

    if (input.value != document.getElementById('re-enter-password').value) {
        input.setCustomValidity('Passwords should match.');
    } else {
        input.setCustomValidity('');
    }
}

How about adding a class to each input and then: 如何为每个输入添加一个类,然后:

if($(".password").val() == $(".re-enter-password").val()){
    alert("it matches")
} else {
    alert("no match yet");
}

What are you checking for in your PHP script? 您要在PHP脚本中检查什么? Anything in particular that justifies the use of PHP? 有什么特别的理由可以证明使用PHP?

You could do that only with JS, you don't need the AJAX part. 您只能使用JS来做到这一点,不需要AJAX部分。

HTML : HTML:

<input type="password" id="password">
<input type="password" id="password_cf">    
<div class="result"></div>

JS (jQuery) : JS(jQuery):

$('#password_cf').on('keyup', function(){     

    if($('#password_cf').val()== $('#password').val())
        $('.result').html('They match');
    else
        $('.result').html('They do not match');
});

Fiddle : http://jsfiddle.net/2sapjxnu/ 小提琴: http : //jsfiddle.net/2sapjxnu/

You can use the blur event if you want to only check once the focus is lost on that field. 如果只想检查焦点在该字段上丢失的焦点,则可以使用blur事件。 It's a bit less "responsive" than verifying on every key, but more performant I guess. 与对每个键进行验证相比,它的“响应性”要差一些,但我想它的性能更高。

Quick and dirty - 又快又脏 -

Given this markup - 鉴于此标记-

<input type="password" name="pw1" />
<input type="password" name="pw2" />

You could check it client side without muliple round trips to the server using code like this - 您可以使用以下代码在客户端进行检查,而无需多次往返服务器:

$('[name="pw2"]').blur(function() {
    var pw1 = $('[name="pw1"]').val();
    var pw2 = $('[name="pw2"]').val();
    if(pw2 != pw1) {
        alert('passwords do not match');
    }
});

Matching 2 form input fields with JavaScript by sending it off to the server to get an assertion response could render a bad user experience, because if you're doing this on each keyPress, then it generates unnecessary internet traffic - while the user is waiting. 通过将2个表单输入字段发送到服务器以获取断言响应来将它们与JavaScript进行匹配可能会带来糟糕的用户体验,因为如果您在每个keyPress上执行此操作,则它将在用户等待时生成不必要的Internet流量。

So, instead, why not match these 2 fields directly with JavaScript? 因此,为什么不直接将这两个字段与JavaScript匹配呢? If you are using a specific regular expression on the server for validation check as well, you can have the server put that regex "pattern" in the HTML fields - (no JavaScrpt needed for that). 如果还要在服务器上使用特定的正则表达式进行验证检查,则可以让服务器在HTML字段中放置该正则表达式“模式”(不需要JavaScrpt)。 Then, onkeyup event you can simply do something like: 然后,onkeyup事件您可以简单地执行以下操作:

form.field2.onkeyup = function()
{
    if (form.field1.value !== form.field2.value)
    {
      /* some code to highlight the 2 fields,
         or show some message, or speech bubble */
      return;
    }
}

form.field1.onkeyup = form.field2.onkeyup;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM