I wanted to hide the confirmation of the password and automatically fill it in from the main text field. Here is the code:

<input type="text" size="30" name="password" id="password" value="" onkeyup="document.forms.registr.confirm_password.value=+this.value;" /> <input type="text" class="text" size="30" name="confirm_password" id="confirm_password" value="" /> 

As a result, when typing the characters a..z in the password field, it appears in confirm_password - NaN, and when entering numbers, everything is fine. I read in the internet about NaN, but I don’t understand why in my case it arises, I’m just copying the value of one field to another. Tell me please?!

    2 answers 2

    It's all about the line
    document.forms.registr.confirm_password.value=+this.value
    For numbers + this.value is equal to this.value, and strings are reduced to numbers - it turns out to be NaN. Swap + and = or just remove +.

    • Thank you for clarifying - chuikoff

    Already answered to me :) "document.forms.registr.confirm_password.value = + this.value;" <- plus before this.value and conceived?

    Use jquery or at least functions, do not directly ... in the code.

    Maybe better this way:

      <script> function do_something(_this) { document.forms.registr.confirm_password.value=_this.value; } // или jquery $(document).ready(function() { $("#password").keyup(function() { $("#confirm_password").val($("#password").val()); }); }); </script> <input type="text" size="30" name="password" id="password" value="" onkeyup="do_something(this);" /> <input type="text" class="text" size="30" name="confirm_password" id="confirm_password" value="" /> 
    • one
      Thanks for the jQuery example - chuikoff
    • Oh, did not immediately notice why the underscore before _this ??? - chuikoff
    • We must somehow call the variable). - ling
    • You're welcome. (slightly corrected jquery example) - City