Active8 months ago
Is there a way using JavaScript to disable the ability to paste text into a text field on an HTML form?
Membuat Input data Barang dengan PHP MYSQL.Tutorial kali ini sebenarnya menyangkut CRUD PHP MYSQL sederhana, Nah Tutorial kali ini admin akan menjelaskan Contoh Script yang digunakan Untuk Input data Barang PHP MYSQL. Dalam pembuatan aplikasi Toko Online, Aplikasi Penjualan / e-comerce dengan PHP, identik dengan Input Data Barang Dengan PHP, Masih banyak yang mencari Cara input data.
E.g.I have a simple registration form where the user is required to input their email twice. The second email entry is to verify there are no typos in the first email entry. However if the user copy/pastes their email then that defeats the purpose and I've been experiencing users having problems because they've input the wrong email and copy/pasted it.
Maybe I wasn't clear on my question but I am not trying to prevent people from copying (or drag selecting) text on their browser. I just want to stop them from pasting input into a text field to minimize user error.
Perhaps instead of using this 'hack' you can suggest another solution to the core problem of what I'm trying to solve here? I've done less than half a dozen user tests and this has already happened twice. My audience does not have a high level of computer proficiency.
Michał Perłakowski50.7k1616 gold badges116116 silver badges133133 bronze badges
justinljustinl5,3041818 gold badges6363 silver badges8585 bronze badges
22 Answers
I recently had to begrudgingly disable pasting in a form element. To do so, I wrote a cross-browser* implementation of Internet Explorer's (and others') onpaste event handler. My solution had to be independent of any third-party JavaScript libraries.
Here's what I came up with. It doesn't completely disable pasting (the user can paste a single character at a time, for example), but it meets my needs and avoids having to deal with keyCodes, etc.
To make use of this in order to disable pasting:
* I know oninput isn't part of the W3C DOM spec, but all of the browsers I've tested this code with—Chrome 2, Safari 4, Firefox 3, Opera 10, IE6, IE7—support either oninput or onpaste. Out of all these browsers, only Opera doesn't support onpaste, but it does support oninput.
Note: This won't work on a console or other system that uses an on-screen keyboard (assuming the on-screen keyboard doesn't send keys to the browser when each key is selected). If it's possible your page/app could be used by someone with an on-screen keyboard and Opera (e.g.: Nintendo Wii, some mobile phones), don't use this script unless you've tested to make sure the on-screen keyboard sends keys to the browser after each key selection.
Christopher ParkerChristopher Parker3,25822 gold badges2222 silver badges3232 bronze badges
Don't do it. Don't mess with the user's browser. By Copy + Pasting into an E-Mail confirmation field, the user accepts responsibility over what they type. If they are dumb enough to copy + paste a faulty address (it has happened to me) then it's their own damn fault.
If you want to make sure that the E-Mail confirmation works out, have the user check their E-Mail while your site waits ('Please open your webmail program in a new window'). Show the E-Mail address in big fat letters ('The confirmation E-Mail was sent to.... made an error? CLick here to change).
Even better, if you can, let the user have some kind of limited access without confirming. That way, they can log in straight away and you improve your chances to keep in touch with the visitor even if the confirmation mail is blocked due to other reasons (e.g. spam filters).
Pekka 웃Pekka 웃370k121121 gold badges866866 silver badges10271027 bronze badges
Add a class of 'disablecopypaste' to the inputs you want to disable the copy paste functionality on and add this jQuery script
BoycsBoycs4,77922 gold badges2424 silver badges2222 bronze badges
Just got this, we can achieve it using
onpaste:'return false'
, thanks to: http://sumtips.com/2011/11/prevent-copy-cut-paste-text-field.htmlWe have various other options available as listed below.
KailasKailas4,93622 gold badges2929 silver badges5656 bronze badges
You can..... but don't.
You should not be altering the default behaviour of a users browser. It really is bad usability for your web application. Also if a user wants to disable this hack then they can just disable javascript on their browser.
Just add these attributes to the textbox
SheffSheff2,33733 gold badges2828 silver badges3535 bronze badges
Crazy idea: Require the user to send you an email as part of the signup process. This would obviously be inconvenient when clicking on a mailto link doesn't work (if they're using webmail, for example), but I see it as a way to simultaneously guarantee against typos and confirm the email address.
It would go like this: They fill out most of the form, entering their name, password, and whatnot. When they push submit, they're actually clicking a link to send mail to your server. You've already saved their other information, so the message just includes a token saying which account this is for.
Josh LeeJosh Lee126k2626 gold badges228228 silver badges249249 bronze badges
How about sending a confirmation email to the email address that the user has just entered twice in which there is a link to a confirmation URL on your site, then you know that they have got the message?
Anyone that doesn't click to confirm the receipt of the email may have entered their email address incorrectly.
Not a perfect solution, but just some ideas.
Richard LucasRichard Lucas54011 gold badge33 silver badges1414 bronze badges
Community♦
Sunil GargSunil Garg5,27499 gold badges6060 silver badges9090 bronze badges
Extending @boycs answer, I would recommend also using 'on'.
Community♦
e3matheuse3matheus1,85911 gold badge1818 silver badges2828 bronze badges
if you have to use 2 email fields and are concerned about the user incorrectly pasting the same mistyped email from field 1 to field 2 then i'd say show an alert (or something more subtle) if the user pastes something into the second email field
this way you don't disable paste, you just give them a friendly reminder to check what they've presumably typed in the first field and then pasted to the second field is correct.
however, perhaps a single email field with autocomplete on is all that's needed. chances are they've filled their email in correctly before on another site at some point and the browser will suggest to fill the field with that email
shunryu111shunryu1113,18333 gold badges1818 silver badges1414 bronze badges
You could attach a 'keydown' listener to the input box to detect whether or not the Ctrl + V keys are being pressed and, if so, stop the event or set the input box's value to '.
That wouldn't handle right clicking and pasting or pasting from the Edit menu of the browser, though. You may need to add a 'last length' counter to the keydown listener and use an interval to check the field's current length to see if it increase since the last keystroke.
Neither is recommended, though. Form fields with paste disabled are extremely frustrating. I'm capable of typing my email correctly the first time, so I reserve the right to paste it into the second box.
KevinKevin2,80322 gold badges2424 silver badges3838 bronze badges
Add a second step to your registration process. First page as usual, but on reload, display a second page and ask the email again. If it's that important, the user can handle it.
AlsciendeAlsciende22.1k77 gold badges4444 silver badges6666 bronze badges
Some may suggest using Javascript to capture the users' actions, like right-clicking the mouse or the Ctrl+C / Ctrl+V key combinations and then stopping the operation. But this is obviously not the best or simplest solution.The solution is integrated in the input field properties itself together with some event capturing using Javascript.
In order to disabled the browsers' autocomplete, simply add the attribute to the input field. It should look something like this:
And if you want to deny Copy and Paste for that field, simply add the Javascript event capturing calls oncopy, onpaste, and oncut and make them return false, like so:
The next step is using onselectstart to deny the input field's content selection from the user, but be warned: this only works for Internet Explorer. The rest of the above work great on all the major browsers: Internet Explorer, Mozilla Firefox, Apple Safari (on Windows OS, at least) and Google Chrome.
vaichidrewarvaichidrewar6,0521313 gold badges5555 silver badges8484 bronze badges
Check validity of the MX record of the host of the given email. This can eliminate errors to the right of the @ sign.
You could do this with an AJAX call before submit and/or server side after the form is submitted.
maxmax49.6k1010 gold badges6868 silver badges110110 bronze badges
Damjan PavlicaDamjan Pavlica12.5k55 gold badges3535 silver badges5555 bronze badges
With Jquery you can do this with one simple codeline.
HTML:
Code:
JSfiddle: https://jsfiddle.net/ZjR9P/2/
JuliusJulius
what about using CSS on UIWebView? something like
also you can read detail about block copy-paste using CSShttp://rakaz.nl/2009/09/iphone-webapps-101-getting-safari-out-of-the-way.html
user275790user275790
Simple solution: just reverse the registration process: instead of requiring confirmation at the end of registration process, request confirmation at the beginning of it! I.e. the registration process started with a simple form asking for e-mail address and nothing else. Upon submitting, an e-mail with link to a confirmation page unique to the e-mail address sent out. The user go to that page, then the rest of information for the registration (user name, full name, etc.) will be requested.
This is simple since the website does not even need to store anything before confirmation, the e-mail address can be encrypted with a key and attached as part of the confirmation page address.
S P Arif Sahari WibowoS P Arif Sahari Wibowo
I did something similar to this for http://bookmarkchamp.com - there I wanted to detect when a user copied something into an HTML field. The implementation I came up with was to check the field constantly to see if at any time there was suddenly a whole lot of text in there.
In other words: if once milisecond ago there was no text, and now there are more than 5 chars... then the user probably pasted something in the field.
If you want to see this working in Bookmarkchamp (you need to be registered), paste a URL into the URL field (or drag and drop a URL in there).
Ivan MaederIvan Maeder
The way that I would resolve the issue of confirming an email address is as follows:
- Before going through the main process - say registering the user - first ask them to enter their email address.
- Generate a unique code and send it to that email address.
- If user has entered the correct email address, they will get the code.
- User must enter that code along with their email address, and their other required information, so they can complete the registration. - Please note that if this time they enter a wrong email address (or a wrong code), because it will not match with the code, the registration will not go through, and the user will be informed right away.
- If the email address, the code, and other registration information have been entered correctly, the registration is complete and user can start using the system immediately. - no need to to respond to any other email address in order to activate their account
For better security, the code should have a limited lifetime, and it should be allowed only once in the registration process.Also, in order to prevent any malicious robot applications, it is better to accompany the first step with captcha or a similar mechanism.
MajixMajix
Using jquery, you can avoid copy paste and cut using this
KrishnaKrishna12.6k55 gold badges3535 silver badges5454 bronze badges
Jignesh VachhaniJignesh Vachhani
protected by Community♦Sep 14 '11 at 0:03
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged javascripthtmlcopy-paste or ask your own question.
Active2 months ago
Okay--I know this is a really elementary issue, but I can't figure it out. This is a question regarding Laravel.
Basically, I have my stylesheets embedded in my default layout view. I'm currently just using regular css to link them, such as:
It works great when I am at a single level route such as /about, but stops working when I go deeper, such as /about/me.
If I look at Chrome's developer console I see some of the following errors (only for the deeper routes):
So clearly it is now looking for the css inside the 'about' folder--which of course isn't a folder at all.
I just want it to look in the same place for the assets regardless of the route.
PetePete2,81566 gold badges2727 silver badges5050 bronze badges
19 Answers
For Laravel 4 & 5:
URL::asset
will link to your project/public/
folder, so chuck your scripts in there..blade.php
extension.someOne2,32322 gold badges1010 silver badges1818 bronze badges
gangan2,52022 gold badges1414 silver badges3232 bronze badges
Laravel 4
The better and correct way to do this
Adding CSS
HTML::style will link to your project/public/ folder
Adding JS
HTML::script will link to your project/public/ folder
Ihab Shoully2,11111 gold badge1414 silver badges2020 bronze badges
mXXmXX1,9431010 gold badges3737 silver badges5555 bronze badges
You are using relative paths for your assets, change to an absolute path and everything will work (add a slash before 'css'.
Alexandre DanaultAlexandre Danault7,27333 gold badges2626 silver badges3131 bronze badges
I supuse you're using Laravel 3, if yes put your CSS/JS files in public folder like
and call it using Blade templates
Fernando MontoyaFernando Montoya2,33511 gold badge1616 silver badges2020 bronze badges
in Laravel 5,
there are 2 ways to load a js file in your view
first is using html helper, second is using asset helpers.
to use html helper you have to first install this package via commandline:
there are 2 ways to load a js file in your view
first is using html helper, second is using asset helpers.
to use html helper you have to first install this package via commandline:
then you need to reqister it, so go to config/app.php, and add this line to the providers array
then you have to define aliases for your html package so go to aliases array in config/app.php and add this
now your html helper is installed so in your blade view files you can write this:
this will look for your test.js file in your project_root/public/js/test.js.
//////////////////////////////////////////////////////////////
to use asset helpers instead of html helper, you have to write sth like this in your view files:
//////////////////////////////////////////////////////////////
to use asset helpers instead of html helper, you have to write sth like this in your view files:
this will look for test.js file in project_root/resources/assets/test.js
SalarSalar3,15522 gold badges3131 silver badges4848 bronze badges
i suggest you put it on route filter before on {project}/application/routes.php
and using blade template engine
or more on laravel managing assets docs
Andry YosuaAndry Yosua
in
laravel 4
you just have to paste {{ URL::asset('/') }}
this way: It is the same for:
zoranc2,31911 gold badge1616 silver badges3434 bronze badges
fravemelfravemel
Unheilig12.4k1616 gold badges5757 silver badges9090 bronze badges
GsubGsub
Best way in my opinion add BASE tag in your HTML
So it's not necessary to use things like
just type
in your view and it will works.
This mehod will deal with RESTful URLs and static resources as images, css, scripts.
vinsavinsa
Vinsa almost had it right you should add
and scripts should go in their regular path
the reason for this is because Images and other things with relative path like image source or ajax requests won't work correctly without the base path attached.
CptmaxonCptmaxon
In Laravel 5.7, put your CSS or JS file into Public directory.
For CSS:
For JS:
ayaio61.5k2020 gold badges138138 silver badges202202 bronze badges
Innocent TRA BIInnocent TRA BI
If you do hard code it, you should probably use the full path (
href='http://example.com/public/css/app.css'
). However, this means you'll have to manually adjust the URLs for development and production.An Alternative to the above solutions would be to use
<link href='URL::to_asset('css/app.css')' />
in Laravel 3 or <link href='URL::asset('css/app.css')' />
in Laravel 4. This will allow you to write your HTML the way you want it, but also let Laravel generate the proper path for you in any environment. mckendricksmckendricks
BIBIN K ONANKUNJUBIBIN K ONANKUNJU6,57866 gold badges3939 silver badges5151 bronze badges
Suppose you have not renamed your public folder. Your css and js files are in css and js subfolders in public folder. Now your header will be :
Hafsul MaruHafsul Maru
Just Add Another Issue:
If you want to remove
/public
from your project using .htaccess
, then you can use this, [Add
public
before /css
inside asset()
][Sometimes, it is useful.]
Maniruzzaman AkashManiruzzaman Akash
For Laravel 4: {!! for a double curly brace { {
and for Laravel 5 & above version: you may replace {!! by {{ and !!} by }} in higher-end version
and for Laravel 5 & above version: you may replace {!! by {{ and !!} by }} in higher-end version
If you have placed JavaScript in a custom defined directory.
For instance, if your
For instance, if your
jQuery-2.2.0.min.js
is placed under the directory resources/views/admin/plugins/js/
then from the *.blade.php
you will be able to add at the end of the section asSince Higher-End version supports Lower-End version also and but not vice-versa
Nɪsʜᴀɴᴛʜ ॐNɪsʜᴀɴᴛʜ ॐ85933 gold badges1212 silver badges2525 bronze badges
ManishaManisha
ocecelioceceli
put your script file in public directory then use(for example for userFunctions.js)
user10248738