I was helping a fellow daniweb user with validation for restricting output to only numerics.
Then another user said even it validates a user could copy and paste into the cell.
Now that has been annoying me and I have been trying to figure out a way to prevent this.

Any Ideas? Thanks, Regards X

<html>
<head>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> 
function Numbers(e) {
var cc = (e.which) ? e.which : event.keyCode;
if (cc > 47 && cc < 58) {
return true;
}
return false;
} 
</SCRIPT>
</head>
<body>
<form method="">
<input type="text" name="jtitle" onKeyPress="return Numbers(this)"
size="57">
</body>
</html>

Recommended Answers

All 6 Replies

woo that was me :P

Maybe there is a way to stop the user from pressing the control key (maybe display an alert box when they press control in the box) obviousley the same applies to right-click and paste, and edit then paste (from the toolbar) i dont know how to get around those with javascript.

You don't have the right to take over the user's computer.

And there are three other ways to paste without using the keyboard.

Instead, you should make your script reject any non-numeric input and ask for a correct entry.

Booong! "Entry must be a number" {OK}

It has nothing to do with taking over the user's computer...

I just dont wish for input not to be copy n pasted into the textfield of MY website...

So this is not possible via javascript?

yes we can do it in javascript
this is the method to be called

function noCTRL(e)
{
var code = (document.all) ? event.keyCode:e.which;
var ctrl = (document.all) ? event.ctrlKey:e.modifiers & Event.CONTROL_MASK;
var msg = "Sorry, this functionality is disabled.";
if (document.all)
{
if (ctrl && code==86) //CTRL+V
{
alert(msg);
window.event.returnValue = false;
}
else if (ctrl && code==67) //CTRL+C (Copy)
{
alert(msg);
window.event.returnValue = false;
}
}
else {
if (ctrl==2) //CTRL key
{
alert(msg);
return false;
}
}
} 


Password :<input type="text" value=""/><br/>
    Confirm Password :<input type="text" value="" onkeydown="return noCTRL(event)"/>

if this answer helps you please mark as solved

Attach an event to the onchange property that will check the whole value of the input, not just the key pressed.

It would be slow, and decrease usability a lot. I'd recommend something along the lines of what MidiMagic said. Like attaching an event to onblur or the form submission etc...

> Now that has been annoying me and I have been trying to figure out a way to prevent this.
Doing so wouldn't be a good idea unless it's absolutely necessary. Sure you can do it, but it won't be worth the kind of computational complexity it introduces.

Long gone are the days of client side validation. Just do server side validation and you won't have to worry about what the user pastes or writes in the text box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.