...

View Full Version : read only text field?



nikko50
05-10-2007, 06:27 PM
Hi there. Is it possible to allow a user to only delete what is in a text field and not enter items into it??

Philip M
05-10-2007, 06:42 PM
Do you mean delete ALL the content of the field, or just part of it?

If the former you could do something like

input type = "text" name = "field1" value = "Something" onclick="scrub(this)"

function scrub(which) {
which.value = "";
}

You could prevent the content being added to by checking the length and if greater than the original (or revised) length alert a message and return false.

<SCRIPT type = "Text/Javascript">
var maxlen = 9 // length of original text e.g. 'Something'
function checklen (which) {
newlen = which.value.length;
if newlen > maxlen {
alert ("You may only delete text from this field, not add more text to it")
return false;
}
else {maxlen = newlen} // maxlen can reduce but not increase.
}
</SCRIPT>

Note that this does not prevent the user from replacing the original text with some other text of equal length.

smalldog
05-11-2007, 12:46 AM
Hi Nikko.. maybe you want something like this :



<script>
function KeyDownHandler(event)
{
if ((event.keyCode == 8) || (event.keyCode == 46))
return true;
else
return false;
}
</script>
<input value="testtadsasdasdext" type="text" onkeypress="return KeyDownHandler(event);">


But it's not so good how it looks, because if javascript is disabled in browser you have a problem :)

rwedge
05-11-2007, 02:06 AM
You can still paste to the text field with this
if ((event.keyCode == 8) || (event.keyCode == 46))

smalldog
05-11-2007, 07:20 AM
You can still paste to the text field with this
if ((event.keyCode == 8) || (event.keyCode == 46))

You can still disable javascript in the browser

Philip M
05-11-2007, 07:42 AM
You can still disable javascript in the browser

Well, that applies to any script. In that case all bets are off. Can be overcome with <NOSCRIPT> tags to redirect to another page.

Why (event.keyCode == 46)? ASCII 46 is a period.

smalldog
05-11-2007, 08:19 AM
Why (event.keyCode == 46)? ASCII 46 is a period.

In Firefox is 46 delete key, it seems in IE is 46 a period



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum