简体   繁体   中英

How to copy input field value (type=password)?

How to copy the value of input field [type=password]?

This is my scenario ,I need to copy the value of password field and paste them in confirm password field in my signup page.

Password: <input type="password" name="pwd">
Confirm Password: <input type="password" name="Confirmpwd">

Actually,my question is how to copy the value in input field.(Using:ctrl+c or mouse right click copy option)

   // get the 2 elements
    var p1 = document.querySelector('input[name="pwd"]');

    var p2 = document.querySelector('input[name="Confirmpwd"]');

      // assign onchnage handler
       p1.onchange = function(){
       //get p1 value
        var val = this.value;
       // assign val to p2
        p2.value = val;
    };

The Copy/Past should not be happen in the password field type for security reasons, the logic is to confirm that you enter the password two times correctly without copy and past it again!

Any way you may copy the value using the following code:

var input = document.getElementById('pwd');
var password = input.value;
text.value=password ;

I strongly advice you to not do this, and let the user to insert the password and confirm it again.

// GET THE VALUE
var pw = $('input[name=pwd]').val();
// SET IT IN THE OTHER INPUT
$('input[name=Confirmpwd]').val( pw );

The cuestion here is, ¿Why? , that's not a "confirmation", just a copy/paste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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