1

I use this code in my login panel in MVC3:

@Html.PasswordFor( m => m.Password, new { maxlength = 10 } )

I want to do that, somebody cannot to PASTE any password to PasswordFor.

(To enter password ONLY with typing)

what must i do?

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Ceyhun Rehimov
  • 111
  • 2
  • 11
  • 5
    You don't. Using copy&paste is the users choice. – CodesInChaos Feb 19 '12 at 21:51
  • i saw this in some sites login panel. when i copy/paste my password. system doesnt know password. i had to write password only by hand. – Ceyhun Rehimov Feb 19 '12 at 21:54
  • 1
    possible duplicate of [Disable Copy/Paste into HTML form using Javascript](http://stackoverflow.com/questions/1226574/disable-copy-paste-into-html-form-using-javascript). Also, this is completely unrelated to C#, ASP.NET and ASP.NET MVC. It's important that you realize that this is a browser/JavaScript/DOMEvent issue. – bzlm Feb 19 '12 at 21:56
  • 4
    Please let us know what site you are developing so we can be sure to never ever go there. – Bryan Watts Feb 19 '12 at 21:58

3 Answers3

7

You probably shouldn't do as it completely sucks from user's perspective but if you want your users to hate you, you could do this:

$('#Password').bind('paste', function(e) {
    e.preventDefault();
});​
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Something like:

var pw = document.getElementById("Password");
pw.addEventListener("paste", function(event)
{
   event.preventDefault();
}

... with equivalent event listening and default prevention for IE etc.

jQuery:

$("#Password").bind("paste", function(event) 
{
   event.preventDefault(); 
});

Neither will work on Opera.

But then, you shouldn't be doing it in any event. My personal way of "pasting" passwords is way more secure than you tampering with my browser's default user experience. And you're messing with my method, which is likely to make me pick a less secure password specifically for your site. Or not use it.

JimmiTh
  • 7,389
  • 3
  • 34
  • 50
0

You can use like this

$(document).ready(function(){
  $('#Password').live("paste",function(e) {
      e.preventDefault();
  });
}); 
Deepakmahajan
  • 856
  • 1
  • 11
  • 23