0

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.

Dendromaniac
  • 378
  • 1
  • 14
  • [This response](http://stackoverflow.com/a/12878395) to another question seems like a reasonable approach. – cwk Apr 11 '15 at 17:37
  • "*Check out this fiddle for the code.*" - no, include the code in your question, otherwise it will likely be closed as off-topic. – David Thomas Apr 11 '15 at 17:38
  • @cwk I have looked at things like that, and I am thinking about it. But I really cannot understand what I am doing wrong with this, as far as I can see it should work... If I can't get a solution to the snippet I will look into other solutions. – Dendromaniac Apr 11 '15 at 17:39
  • @David Thomas Will do, I will edit it now. – Dendromaniac Apr 11 '15 at 17:43
  • If it is always 1 character off why not just add or substract 1 to get to wherever you need? – PeeHaa Apr 11 '15 at 17:49
  • @PeeHaa Because no matter what I put, it's always of by 1, Even if put 10. That is what is confusing me. – Dendromaniac Apr 11 '15 at 17:52

0 Answers0