Skip to content Skip to sidebar Skip to footer

How To Make Some Piece Of Text Inside A Input Field Non Editable?

I have a requirement that I have a input field of maxLength 4.among these 4 characters first 2 characters will be 'FR'.Remaining 2 character will be inserted by user.So,during the

Solution 1:

A hacky solution, but kinda does the trick using JS.

<input id="myId" type="text" value="AZ"></input>

$("#myId").keydown(function(event){
    console.log(this.selectionStart);
    console.log(event);
    if(event.keyCode == 8){
        this.selectionStart--;
    }
    if(this.selectionStart < 2){
        this.selectionStart = 2;
        console.log(this.selectionStart);
        event.preventDefault();
    }
});

$("#myId").keyup(function(event){
    console.log(this.selectionStart);
    if(this.selectionStart < 2){
        this.selectionStart = 2;
        console.log(this.selectionStart);
        event.preventDefault();
    }
});

Fiddle here!

Post a Comment for "How To Make Some Piece Of Text Inside A Input Field Non Editable?"