Using JQuery to force cursor to start on the third character position of a text field

I am trying to find a way so that when a user clicks on a form text field I would like the cursor to only start after the third character within the text field.

The reason for this is that I need the first 3 characters to be uneditable so the user can only start typing after the third character. It must not be possible to click on the first 3 characters.
mike99cAsked:
Who is Participating?
 
F PCommented:
0
 
F PCommented:
So, why don't you set it to static text in a label outside of the field, and then parse whatever they input in with the form submission in javascript to add those first 3 in at that point, or on the side handling the db to make sure that the first 3 have that value, if not add it?
0
 
F PCommented:
<label for="textValue">ABC</label> <input type="text" name="textValue" id="textValue" value="DEFG"> 

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mike99cAuthor Commented:
OK I can see in the example given that you initialise the text field with "DEFG". But if the user edits the text field they can still delete the text.

For my requirement I want to add the first 3 characters say ABC in the text field. I then want the user to only edit the text field AFTER the ABC and not be able to delete the ABC.
0
 
F PCommented:
Right, so in my example, you'll notice that the ABC is not editable since it's a label. Just for the value to appear in front of the input, I put it there, but in reality if the user doesn't need to edit the characters, they don't need to appear in the textbox. I would split the string, and only only that part of the value that they can edit. I hope that clarifies a bit.
0
 
mike99cAuthor Commented:
Thanks Frank but what I am really looking for is a JQuery solution in which the first 3 characters appear within the text field itself and the cursor is forced to start after the 3rd character. I did originally think of a similar solution to yours but this post is about keeping the 3 characters within the text field.
0
 
F PCommented:
Well, I found this which seems to do exactly what you're asking for in the jQuery only...

https://gist.github.com/DrPheltRight/1007907

... But, don't forget you still need to validate the input on the backend before you submit it to the database. Javascript can always be disabled prior to hitting submit or POSTing the values.
0
 
F PCommented:
Capture keydowns and mouseclick events to validate the position with that and move accordingly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.