I would like to have a text input field containing the "$" sign in the very beginning,开发者_开发问答 and no matter what editing occurs to the field, for the sign to be persistent.
I would be good if only numbers were accepted for input, but that's just a fancy addition.
Consider simulating an input field with a fixed prefix or suffix using a span with a border around a borderless input field. Here's a basic kickoff example:
.currencyinput {
border: 1px inset #ccc;
}
.currencyinput input {
border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>
Use a parent .input-icon
div. Optionally add .input-icon-right
.
<div class="input-icon">
<input type="text">
<i>$</i>
</div>
<div class="input-icon input-icon-right">
<input type="text">
<i>€</i>
</div>
Align the icon vertically with transform
and top
, and set pointer-events
to none
so that clicks focus on the input. Adjust the padding
and width
as appropriate:
.input-icon {
position: relative;
}
.input-icon > i {
position: absolute;
display: block;
transform: translate(0, -50%);
top: 50%;
pointer-events: none;
width: 25px;
text-align: center;
font-style: normal;
}
.input-icon > input {
padding-left: 25px;
padding-right: 0;
}
.input-icon-right > i {
right: 0;
}
.input-icon-right > input {
padding-left: 0;
padding-right: 25px;
text-align: right;
}
Unlike the accepted answer, this will retain input validation highlighting, such as a red border when there's an error.
JSFiddle usage example with Bootstrap and Font Awesome
You can wrap your input field into a span, which you position:relative;
. Then you add with
:before
content:"€"
your currency symbol and make it position:absolute
. Working JSFiddle
HTML
<span class="input-symbol-euro">
<input type="text" />
</span>
CSS
.input-symbol-euro {
position: relative;
}
.input-symbol-euro input {
padding-left:18px;
}
.input-symbol-euro:before {
position: absolute;
top: 0;
content:"€";
left: 5px;
}
Update If you want to put the euro symbol either on the left or the right side of the text box. Working JSFiddle
HTML
<span class="input-euro left">
<input type="text" />
</span>
<span class="input-euro right">
<input type="text" />
</span>
CSS
.input-euro {
position: relative;
}
.input-euro.left input {
padding-left:18px;
}
.input-euro.right input {
padding-right:18px;
text-align:end;
}
.input-euro:before {
position: absolute;
top: 0;
content:"€";
}
.input-euro.left:before {
left: 5px;
}
.input-euro.right:before {
right: 5px;
}
Since you can't do ::before
with content: '$'
on inputs and adding an absolutely positioned element adds extra html - I like do to a background SVG inline css.
It goes something like this:
input {
width: 85px;
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='16px' width='85px'><text x='2' y='13' fill='gray' font-size='12' font-family='arial'>$</text></svg>");
padding-left: 12px;
}
It outputs the following:
Note: the code must all be on a single line. Support is pretty good in modern browsers, but be sure to test.
I ended up needing the type to still remain as a number, so used CSS to push the dollar sign into the input field using an absolute position.
<div>
<span style="position: absolute; margin-left: 1px; margin-top: 1px;">$</span>
<input type="number" style="padding-left: 8px;">
</div>
If you only need to support Safari, you can do it like this:
input.currency:before {
content: attr(data-symbol);
float: left;
color: #aaa;
}
and an input field like
<input class="currency" data-symbol="€" type="number" value="12.9">
This way you don't need an extra tag and keep the symbol information in the markup.
$<input name="currency">
See also: Restricting input to textbox: allowing only numbers and decimal point
Put the '$' in front of the text input field, instead of inside it. It makes validation for numeric data a lot easier because you don't have to parse out the '$' after submit.
You can, with JQuery.validate() (or other), add some client-side validation rules that handle currency. That would allow you to have the '$' inside the input field. But you still have to do server-side validation for security and that puts you back in the position of having to remove the '$'.
Another solution which I prefer is to use an additional input element for an image of the currency symbol. Here's an example using an image of a magnifying glass within a search text field:
html:
<input type="image" alt="Subscribe" src="/graphics/icons/search-button.png">
<input type="text" placeholder="Search" name="query">
css:
#search input[type="image"] {
background-color: transparent;
border: 0 none;
margin-top: 10px;
vertical-align: middle;
margin: 5px 0 0 5px;
position: absolute;
}
This results in the input on the left sidebar here:
https://fsfe.org
Try This
<label style="position: relative; left:15px;">$</label>
<input type="text" style="text-indent: 15px;">
For bootstrap its works
<span class="form-control">$ <input type="text"/></span>
Don't use class="form-control" in input field.
None of these answers really help if you are concerned with aligning the left border with other text fields on an input form.
I'd recommend positioning the dollar sign absolutely to the left about -10px or left 5px (depending whether you want it inside or outside the input box). The inside solution requires direction:rtl on the input css.
You could also add padding to the input to avoid the direction:rtl, but that will alter the width of the input container to not match the other containers of the same width.
<div style="display:inline-block">
<div style="position:relative">
<div style="position:absolute; left:-10px;">$</div>
</div>
<input type='text' />
</div>
or
<div style="display:inline-block">
<div style="position:relative">
<div style="position:absolute; left:5px;">$</div>
</div>
<input type='text' style='direction: rtl;' />
</div>
https://i.imgur.com/ajrU0T9.png
Example: https://plnkr.co/edit/yshyuRMd06K1cuN9tFDv?p=preview
I just used :before with the input and passed $ as the content
input{
margin-left: 20px;
}
input:before {
content: "$";
position: absolute;
}
<style>
.currencyinput {
border: 1px inset #ccc;
border-left:none;
}
.currencyinput input {
border: 0;
}
</style>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" oninput="this.value = this.value.replace(/[^0-9]/.g, ''); this.value = this.value.replace(/(\..*)\./g, '$1');" style="text-align:right;border-right:none;outline:none" name="currency"><span class="currencyinput">%</span>
</body>
</html>
%
.currencyinput {
border: 1px inset #ccc;
padding-bottom: 1px;//FOR IE & Chrome
}
.currencyinput input {
border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>
Yes, if you are using bootstrap, this would work.
.form-control input {
border: none;
padding-left: 4px;
}
and
<span class="form-control">$ <input type="text"/></span>
.currencyinput {
border: 1px inset #ccc;
border-left:none;
}
.currencyinput input {
border: 0;`enter code here`
}
<input type="text" oninput="this.value = this.value.replace(/[^0-9]/.g, ''); this.value = this.value.replace(/(\..*)\./g, '$1');" style="text-align:right;border-right:none;outline:none" name="currency"><span class="currencyinput">%</span>
精彩评论