473,378 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Restrict user to copy/paste Issue

19
Hi all,
I have a form with fields email and confirm email. I want to restrict the user not to copy the value from email field and paste it into the other one.

I think this is possible through javascript, thats why i am posting my problem here.

Anyone who can give some idea on how it can be achieved, please post it here as it is really urgent.

Thanks
Jun 10 '08 #1
8 4761
gits
5,390 Expert Mod 4TB
you could make it harder but you cannot fully avoid it ... for 'ctrl-V' you have to handle the keypress-event, for the contextmenu you could handle the 'oncontextmenu'-event but for the browsers menubar you are lost ...

kind regards
Jun 10 '08 #2
mrhoo
428 256MB
Its never a good idea to fight with your users,
but you can use some indirection here, if you insist.

Save each key's value from the second email field as it is pressed,
in some accessible place- maybe the field's title.

When the form is validated, or the field blurs or changes,
test if the title of the second field matches the value of the first.

If your two email fields are named 'email1' and 'email2',
(I assume that email1 has its own validation for good email syntax,
and there is no title set on email2):
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('email2')[0].onkeypress= function(e){
  2.     e= window.event || e;
  3.     var who= e.target || e.srcElement;
  4.     var c= e.charCode || e.keyCode;
  5.      if(who.value=='') who.title='';
  6.     who.title+=String.fromCharCode(c);
  7.  
  8. }
  9. document.getElementsByName('email2')[0].onchange= function(e){
  10.     var in1= document.getElementsByName('email1')[0];
  11.     var in2= document.getElementsByName('email2')[0];
  12.     if(in1.value && in1.value!= in2.title){        
  13.         in2.title='';
  14.         in2.value='';
  15.         alert('Please type your email again');
  16.         in2.focus();
  17.         return false;
  18.     }
  19.     else alert(in2.title);
  20.     return true;
  21. }
  22.  
Jun 10 '08 #3
gjain12
19
Hi,
Thanks for reply.
I am able to achieve the required functionality using the following code.
Expand|Select|Wrap|Line Numbers
  1. var el; 
  2. onload=function()
  3.     el = document.forms[0].elements; 
  4.     for(var i=0;i<el.length;i++)
  5.     { 
  6.         if(el[i].name=='confirmEmail')
  7.         {
  8.             el[i].onpaste=function(){return false; } 
  9.         } 
  10.     } 
  11. }
But the problem is that it is working only for IE and not for firefox.

Anybody has any idea, what we have to do to make it comapatible to both the browsers.

Thanks
Gaurav


Its never a good idea to fight with your users,
but you can use some indirection here, if you insist.

Save each key's value from the second email field as it is pressed,
in some accessible place- maybe the field's title.

When the form is validated, or the field blurs or changes,
test if the title of the second field matches the value of the first.

If your two email fields are named 'email1' and 'email2',
(I assume that email1 has its own validation for good email syntax,
and there is no title set on email2):
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('email2')[0].onkeypress= function(e){
  2.     e= window.event || e;
  3.     var who= e.target || e.srcElement;
  4.     var c= e.charCode || e.keyCode;
  5.      if(who.value=='') who.title='';
  6.     who.title+=String.fromCharCode(c);
  7.  
  8. }
  9. document.getElementsByName('email2')[0].onchange= function(e){
  10.     var in1= document.getElementsByName('email1')[0];
  11.     var in2= document.getElementsByName('email2')[0];
  12.     if(in1.value && in1.value!= in2.title){        
  13.         in2.title='';
  14.         in2.value='';
  15.         alert('Please type your email again');
  16.         in2.focus();
  17.         return false;
  18.     }
  19.     else alert(in2.title);
  20.     return true;
  21. }
  22.  
Jun 11 '08 #4
acoder
16,027 Expert Mod 8TB
onpaste doesn't work in most browsers, probably only IE.

Have you tried mrhoo's suggested code?
Jun 11 '08 #5
gjain12
19
Hi,

Thanks for reply.
Actually I could not understand the flow of code, i.e.how it will work, posted by mrhoo. And I am not using the plain html code ( I am using a tool SIM 7.1), thats why I have to look into how will it work in my case and that is possible only if I can understand the code.

If you,mrhoo or anyone else can elaborate it, then it will be really helpful to me.

Thanks
Gaurav

onpaste doesn't work in most browsers, probably only IE.

Have you tried mrhoo's suggested code?
Jun 11 '08 #6
mrhoo
428 256MB
If you have a handler on the second text box that adds the letter from each of its keydowns to a value (the box's empty title is easily read and written to), it doesn't matter what they do to fill the box.

The built up title will only match if each character was pressed in turn.

If you paste the address, the most you would get is a 'v', if pasted with the key board, and nothing at all if pasted using the mouse menu.
Jun 11 '08 #7
eshka
2
To prevent pasting to text field, my idea was to limit content length using 'maxlength'. Increase 'maxlength' by 1 only if a key is pressed.
So:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" onkeydown="checkItNoPaste(event);" />
Expand|Select|Wrap|Line Numbers
  1. function checkItNoPaste(evt){
  2.    evt = (evt) ? evt : window.event;
  3.    input= evt.target || evt.srcElement;
  4.    input.setAttribute('maxlength', input.value.length + 1); 
  5. }
So if user tries to paste using Ctrl+V or Ctrl+Insert, it pastes only 1 symbol.
And it's impossible to paste using context menu.
Nov 13 '08 #8
serdar
88
I think this is possible through javascript, thats why i am posting my problem here.Thanks
What if they turn off javascript, how would you restrict then?
Nov 15 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
4
by: Rik Hemsley | last post by:
Hi, Our web application impersonates a domain user when it runs. Usually, the printers visible to the application are the same as those visible to the domain user. At one installation, the...
17
by: emma.sax | last post by:
Hi all, I have a form where we would like the user to input their email address twice, to ensure they've typed it correctly, as is found on most sign-ups I'm looking for a solution to the...
2
by: sureshrkumar | last post by:
I want to restrict Cut + Copy + Paste option in a web application(when i right click in a browser).I need the code for IE and Netscape Regards Suresh
1
by: Albin | last post by:
Hi, I have a form in which i have two text boxes one for entering email and the other for verify email (to make sure the user entered the correct email id we are asking to enter the same email...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
1
by: lrw0831 | last post by:
I have the main form where employees enter "issues". Some issues have fields that are the same. My fields are Issue ID (auto) Product Entered BY Issue Type Priority Request from Status...
6
by: lrw0831 | last post by:
I have the main form where employees enter "issues". Some issues have fields that are the same. My fields are Issue ID (auto) Product Entered BY Issue Type Priority Request from Status...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.