Controlling characters when pasting

Can anybody advise me on how to control the characters when pasting?

I have the following Javascript for example that limits characters to numbers only, which works good;

function accept_alphanumeric_only(e, t) {
var regexp = new RegExp(/^[a-zA-Z0-9_.,]*$/);
if (window.event) {keynum = e.keyCode;}
else if (e.which) {keynum = e.which;}
var result = regexp.test(String.fromCharCode(keynum));
return result;}

class=“length”><input type=“text” maxlength=“8” value="<?php echo isset($_POST['lng']) ? $_POST['lng'] : '';?>" name=“lng” onkeypress=“return accept_numeric_only(event, this);” onPaste=“return accept_numeric_only(event, this);” style="width: 100%; height: 3.9mm; "

But I want onPaste to work the same, but the above code does not work.

Im not sure on how to wrap the coding, so please advise if the above is not correct.

Well, you might just want to use the “paste” function instead of onPaste call.
Here is a page from Mozilla that explains it. It has two example input fields.
One of them forces all caps to the text when pasted into it. You can alter that to handle numbers as needed.
JS Paste-Event
Hope this helps!

Thanks @ErnieAlex. Ive tried “paste” only but nothing works.

It appears the link that you provided is on a different wavelength than my project. The JS Im working with "accept"s designated characters whereas that link "override"s.

The guy who provided the “onkeypress” coding wanted to build my entire website, he must have thought we had an endless supply of funds, & after he didnt get the job he would not provide anymore information, so Im really looking for an expert like him to complete the process.

Well, you are talking about BROWSER-side or CLIENT-side controlling of input fields.
That is a bit silly because it can be altered by a user since it is inside the browser.

Normally, you validate characters after they are posted to the server to insure they are valid and if not, you throw out an error message to the user that only certain characters are valid. You are attempting to alter pasted values. Does this mean you have large text-area’s that you allow users to post into and want to fix them PRE-POST? Please tell us what your website is about and why you would need this odd type of character protection. Unsure how to help you with this.

Its just a client creating an advert & I dont want coding characters, or non applicable characters, inserted or pasted. I intend to refuse proceeding if JS is turned off, but Im yet to learn that.

I dont know how to “validate characters after they are posted”. But the system Im using wont involve any error message & is simple.

You can’t tell a user whey they posted something wrong? Odd system!

I would suggest that you just use onChange not onPaste. Using Javascript when data is pasted is an event that is caught only once. This means that if the user pastes something and then adds to it, it will not be caught. Using on changes would always catch any change even pastes as a paste would change the data and would be caught. Does that make sense to you?

OK, the system works good with “onkeypress” where if an alpha character is key pressed into a measurement box, nothing will appear. But more importantly if a user key pressed coding characters like []{}" ’ they wont insert. There is similar limitations applying to text areas. Currently I use onPaste=“return false” that forbids all pasting, which means if the user has a significant script he/she will have to write it longhand.

onChange does not appear to suit the need.

Take a look at Contact Brian Gottier & try to insert or paste programming characters & it wont allow. I need something like that.

I asked him for help but is unhelpful other than providing a couple of web links with his coding but I cant understand it.

That is being done with JavaScript in the file

Copy it to a JavaScript beautifier so you can read it easier.

Thanks @benanamen. Can you explain better? I dont know what is a JavaScript beautifier. And does it matter where in the file it is?

I do already have some Javascript in my file & it may conflict. See above.

A beautifier is one that takes compressed code and makes it easier to read.
Here is one example of an online version:
Get your JS from the post Benanamen gave you and put it into the beautifier and you will be able to read it better. Then, you can copy it to your live page and adjust as needed. Hope this helps!

I would prefer to work in reverse & paste some data into my file & see if it does the same as brianswebdesign or something similar, & if it doesnt then look for the reason. Once I get something working I can then try the JavaScript beautifier, & lean from there.

My file contains text boxes & text areas. The former has minimal max characters so dont need any form of control but I would like to control the input of the text areas.

I have copied the script from the link benanamen provided & pasted it in place of the current coding that I have, but that does not work.

By the way, that coding looks nothing like the coding that Brian Gottier provided. The coding he provided was in 2 pages & via the Internet, which leads me to believe his coding referred to those pages to be effective.

Can someone guide me as what to copy & paste & where?

@benanamen I note that you classify yourself as "Master Coder ". Can you advance on the following?

This is my current coding where Im controlling the characters of a text area, and which it disallows pasteing;

div class=“des” textarea maxlength=“800” value=" ?php echo isset($_POST[‘des’]) ? $_POST[‘des’] : ‘’;? " name=“des” onkeypress=“return accept_alphanumchar_only(event, this);” onPaste=“return false” style="width: 100%; height: 28mm; background-color: #C0C0C0; " /textarea /div

And this is what I would like, which controls the onpaste input the same as onkeypress;

div class=“des” textarea maxlength=“800” value=" ?php echo isset($_POST[‘des’]) ? $_POST[‘des’] : ‘’;? " name=“des” onkeypress=“return accept_alphanumchar_only(event, this);” onPaste=“return accept_alphanumchar_only(event, this);” style="width: 100%; height: 28mm; background-color: #C0C0C0; " /textarea /div

Ive changed the greater & lessor symbols to spaces.

Your help would be appreciated.

@benanamen, I have copied the contents of 1 to a JavaScript beautifier, copied the beautifier contents to my file, encased it with the script tags, & changed my text area name to “contactForm”, but NOTHING.

I can not think of anything else that I can do.

I came to this site looking for an expert but appears nobody home.

Are you actually wanting to learn how to code or do you just want to copy.paste something that works?

Save the working page and strip out everything that it doesn’t need to work and study the code. If you look at the JS code you will see it is looking for a specific submit button name which I suspect you didn’t do anything about.

Just an FYI, the debugger in your browsers Dev Tools can also beautify JS code.

Sorry but weve had a few days offline.

Please understand our position. We gained the idea of building a website 20-30 years ago. We searched for developers locally & abroad & all we could find were scammers just wanting money upfront before building anything. So we attempted to start something ourselves but achieved little very slowly. Then 10 years ago we moved country to one having a much larger population, but again found scammers and or incompetence in abundance. We then searched other countries with the idea of maybe moving again but found the same thing. So with the limited information we had gained we decided to build the site ourselves. A partner would be ideal, one who is willing to wait for the rewards but not one has come forward.

The site is now about 75% complete to go online meaning we have completed a large amount of coding, & most of that coding has been copied & pasted mainly to avoid errors. When the site goes online, member developers will be invited to contribute for improvement, & be paid accordingly. Javascript is a very small portion of our project, probably less than 1%. The time to learn Javascript would be way out of proportion in relation to the overall project, so yes we “just want to copy.paste something that works”. That was how we got the “onkeypress” function to work. It is hoped that is not too much to expect.

We have studied the code but we are nowhere proficient to know what to look for as far as a specific submit button name.

Your response is full of red flags. I expect you will not be much further along in the coming 20-30 years.

This forum is for people that want to learn. You have also made it clear you are not going to hire anyone. You will not get anywhere on this forum or any other forum for that matter.

We have got a long way in the last 5 years by going alone & by copying & pasting everything, as Ive said the site is close to going online. The only way to get things done properly is to do it yourself. We have been in business for many years with that attitude. Hiring staff is one major factor in corporate collapse. Another major factor is advertising.

The sites form of hiring people will be much different than conventional hiring, as will most other factors about the site. Being different is what makes any site successful.

We have found many people in forums who are only offering help for the purpose being hired, but those type of people will never be part of our project particularly in the long term.

Those who help now will be significantly rewarded when the site makes money.

Hello, I have some basic web development skills where I can assist you in your project and gain more experience. I’m not expecting any payment from you so don’t expect me to be a master coder.

If you are interested, let me know.

Thanks @PHPNob, you appear to be just the person we are looking for. Can you email me at bensanchez480 [at]

Sponsor our Newsletter | Privacy Policy | Terms of Service