I made this function to prevent the caret from going into the first 8 characters https://
. The idea is to allow users to input a YouTube URL, since all youtube.com urls are https, I am ensuring that it is always there, so far I have made it so none of the characters can be deleted.
Here is what I have so far:
$( "#main-entry-box" ).val("").focus().val("https://").on("keydown", function() {
// This keydown is the relevant code for the question,
// The rest was included in case it was interfering with this.
if (this.selectionStart == this.selectionEnd) {
if (this.selectionStart < 9) {
this.selectionStart = 9;
this.selectionEnd = 9;
}
}
}).on("keyup propertychange input paste", function() {
if (this.value == "https:/") {
this.value = "https://";
}
else if (this.value.substring(0,8) != "https://") {
this.value = "https://" + this.value.substring(7).replace(/^https?:\/\//gi, "");
}
else {
this.value = "https://" + this.value.substring(8).replace(/^https?:\/\//g, "");
}
});
#main-entry-box
is the relevant input.
The idea is to keep the caret after the https://
characters, but it currently only keeps it after the first /
.
It is in a fiddle here too.