Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add an unremovable prefix to an HTML input field?

Tags:

html

jquery

Using jQuery, how can I add a default value of http:// into an input field that can’t be removed, but that still allows you to type a URL after it?

Default: http://
Url: http://www.domain.name

like image 321
Mindtree Avatar asked Dec 27 '10 00:12

Mindtree


People also ask

How do you make an input non editable?

The readonly attribute makes a form control non-editable (or “read only”). A read-only field can't be modified, but, unlike disabled , you can tab into it, highlight it, and copy its contents. Setting the value to null does not remove the effects of the attribute. Instead use removeAttribute('readonly') .

How do you make an input field editable?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

How do I restrict input to numbers in HTML?

To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”


1 Answers

this works well for me:

$("input").keydown(function(e) { var oldvalue=$(this).val(); var field=this; setTimeout(function () {     if(field.value.indexOf('http://') !== 0) {         $(field).val(oldvalue);     }  }, 1); }); 

http://jsfiddle.net/J2BKU/

like image 145
guillaumesmo Avatar answered Sep 16 '22 15:09

guillaumesmo