Solved

How to disable on PART of a textbox?

Posted on 2009-07-04
13
334 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:JaressLoo
13 Comments
 
LVL 5

Expert Comment

by:j_stone
ID: 24778620
it seems like that might create security problems...

any reason you can't just add the "../videos" part after they enter the rest?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24778705
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.
0
 
LVL 1

Author Comment

by:JaressLoo
ID: 24779008
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.Add() 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!!
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; }

  }

}

Open in new window

0
 
LVL 20

Accepted Solution

by:
informaniac earned 125 total points
ID: 24779692
What you could do is. Create an image which contains only the words videos. Provide the background of the textbox as the image and using css provide left padding for the textbox.

This is an alternative solution.

Hope this helps.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24781799
Your "solution" does not work, I can type into the 1st 10 positions and I can paste in anything
0
 
LVL 1

Author Closing Comment

by:JaressLoo
ID: 31599853
This was FAR better than my solution!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:JaressLoo
ID: 24781807
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!
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24781840
this is closer

<script>
function vid(obj){
 if (!/^\.\.\/videos\//.test(obj.value))
   obj.value=obj.value.replace(/^(.{11})/,"../videos/")
}
</script>

<form>
 <input value="../videos/"  onkeyup="vid(this)" onpaste="return false">
</form>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24781843
your "solution" does not work, I tested it

0
 
LVL 1

Author Comment

by:JaressLoo
ID: 24782158
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/
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24782865
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.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24782928
....just found a small problem with mine and new version shld be

function vid(obj){
  if (!/^\.\.\/videos\//.test(obj.value))
         obj.value=obj.value.replace(/^.{0,11}/,"../videos/")
}


Covering all sub cases for these types of problems is notoriously difficult, regardless of what method you use.
0
 
LVL 1

Author Comment

by:JaressLoo
ID: 24783325
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!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now