JaressLoo
asked on
How to disable on PART of a textbox?
I have a textbox where I want the first 10 characters of it to ALWAYS be "../videos/" (without quotes).
I don't want them to be able to delete it in any way, shape, or form. I have seen this done before in another webpage where the developer prevented the user from being able to highlight, delete, or backspace the first 10 characters or so of the textbox...
Any ideas on how to do this?
I am using ASP.NET, but unless there is a server-side control, I would imagine that this would take some sort of javascript in order to make this possible.
I don't want them to be able to delete it in any way, shape, or form. I have seen this done before in another webpage where the developer prevented the user from being able to highlight, delete, or backspace the first 10 characters or so of the textbox...
Any ideas on how to do this?
I am using ASP.NET, but unless there is a server-side control, I would imagine that this would take some sort of javascript in order to make this possible.
The only way would be to have an even handler that gets called when keys are either pressed, or released, and check for thing like delete, or backspace.
It would be better though to just not display this prefix stuff, but automatically prepend it to the value specified by the user in the input field.
It would be better though to just not display this prefix stuff, but automatically prepend it to the value specified by the user in the input field.
ASKER
I ended up just writing a javascript function to take care of this.
I attached the javascript function that I wrote where "e" is the clientID of the control to be evaluated.
I attached this client side function by using the <ControlName>.Attributes.A dd() method in ASP.NET, as follows:
txtVideoUrl.Attributes.Add ("onkeyup" , "checkvalue(" & txtVideoUrl.ClientID & ")")
This took care of my problem pretty well. It still allows them to delete it, but the moment it's deleted, the javascript function adds it right back.
I would show you how this works, but it's a protected page, so I can't...
I mainly wanted to do this for effect. There was no particular reason to do it this way. I could have just as well prepended it to the value of the string once the page posted back, but I wanted to do something cool.
Thanks for your help!!
I attached the javascript function that I wrote where "e" is the clientID of the control to be evaluated.
I attached this client side function by using the <ControlName>.Attributes.A
txtVideoUrl.Attributes.Add
This took care of my problem pretty well. It still allows them to delete it, but the moment it's deleted, the javascript function adds it right back.
I would show you how this works, but it's a protected page, so I can't...
I mainly wanted to do this for effect. There was no particular reason to do it this way. I could have just as well prepended it to the value of the string once the page posted back, but I wanted to do something cool.
Thanks for your help!!
function checkvalue(e) {
var ele = document.getElementById(e);
if (ele.value.substring(0, 10) != '../videos/') {
if (ele.value.length < 10) {
ele.value = '../videos/';
}
else { ele.value = '../videos/' + ele.value; }
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your "solution" does not work, I can type into the 1st 10 positions and I can paste in anything
ASKER
This was FAR better than my solution!
ASKER
I have accepted informaniac's solution as the real solution. While my method does work (at least for Firefox, IE, and Safari on Windows), this solution is a MUCH better idea!
this is closer
<script>
function vid(obj){
if (!/^\.\.\/videos\//.test(o bj.value))
obj.value=obj.value.replac e(/^(.{11} )/,"../vid eos/")
}
</script>
<form>
<input value="../videos/" onkeyup="vid(this)" onpaste="return false">
</form>
<script>
function vid(obj){
if (!/^\.\.\/videos\//.test(o
obj.value=obj.value.replac
}
</script>
<form>
<input value="../videos/" onkeyup="vid(this)" onpaste="return false">
</form>
your "solution" does not work, I tested it
ASKER
This really does seem to work for me. Maybe you can expand on how this doesn't work.
I have created a page where you can test it the way I'm using it. Visit this link to check it out:
http://betatest1.jaressloo.com/textboxtest/
I have created a page where you can test it the way I'm using it. Visit this link to check it out:
http://betatest1.jaressloo.com/textboxtest/
Tested on Firefox/3.0.11 and IE6
(1) Type an x before ../videos/ and you get
../videos/x../videos/
(2) I can over write ../videos/ by simply select all and pasting. Basically can paste anything I like.
(1) Type an x before ../videos/ and you get
../videos/x../videos/
(2) I can over write ../videos/ by simply select all and pasting. Basically can paste anything I like.
....just found a small problem with mine and new version shld be
function vid(obj){
if (!/^\.\.\/videos\//.test(o bj.value))
obj.value=obj.value.replac e(/^.{0,11 }/,"../vid eos/")
}
Covering all sub cases for these types of problems is notoriously difficult, regardless of what method you use.
function vid(obj){
if (!/^\.\.\/videos\//.test(o
obj.value=obj.value.replac
}
Covering all sub cases for these types of problems is notoriously difficult, regardless of what method you use.
ASKER
Changed the link to:
http://betatest1.jaressloo.com/ee/textboxtest/
Your js function works a lot better now! Before, if I just backspaced the 10th character, it didn't do anything.
Anyway, it works better now, but I enabled the paste feature again because I want them to be able to paste. Some of the video names might be pretty lengthy and it would be a pain to not allow that...
Thanks for your help!
http://betatest1.jaressloo.com/ee/textboxtest/
Your js function works a lot better now! Before, if I just backspaced the 10th character, it didn't do anything.
Anyway, it works better now, but I enabled the paste feature again because I want them to be able to paste. Some of the video names might be pretty lengthy and it would be a pain to not allow that...
Thanks for your help!
any reason you can't just add the "../videos" part after they enter the rest?