views:

228

answers:

3

Hello,

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.

Thank you very much in advance!

+1  A: 
 $<input name="currency">

See also: Restricting input to textbox: allowing only numbers and decimal point

David Dorward
Yes, I actually want for the currency sign to be inside the field, but thanks for the input restriction link!
Hristo
+1  A: 

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 '$'.

dnagirl
Thank you! I was aware of that, but I actually needed my currency sign to be inside the text field. There is a better answer below which I believe can be of help to many people.
Hristo
@Hristo: I'm glad you found a solution that suits. But for the record, the one you've marked as the answer does not put your $ **in** the input field. It only makes it **seem** to be there with styling. That said, it's definitely slick!
dnagirl
Yes, it visually solves the problem, while your suggestion is just pointing (yes, still in the right direction), but I wouldn't call it solution. Thank you very much again and remember I didn't mean to offend anybody here!
Hristo
+1  A: 

Consider simulating an input field with a fixed prefix or suffix using a span with a border around a borderless input field:

<span class="currencyinput">$<input type="text" name="currency"></span>

with

.currencyinput {
    border: 2px inset #ccc;
}
.currencyinput input {
    border: 0;
}
BalusC
Thank you very much! This is the solution that did the job for me. Perfect indeed!
Hristo
Yes, I know that. I couldn't do it at first because my reputation score wasn't enough. Now that I have the necessary reputation to vote, I made it clear enough which was the most helpful answer for me.Thanks again!
Hristo