Three general notes:
^[a-zA-Z0-9$]{10}$
- the parentheses are not necessary
{10}+
does not make much sense, drop the plus (there's no need for a possessive quantifier on a fixed count)
- if you want to allow a dollar sign, just add it to the character class
To allow a dollar sign only once, you can use an extended version of the above:
^(?=[^$]*\$[^$]*$)[a-zA-Z0-9$]{10}$
The (?=[^$]*\$[^$]*$)
is a look-ahead that reads
(?= # start look-ahead
[^$]* # any number of non-dollar signs
\$ # a dollar sign
[^$]* # any number of non-dollar signs
$ # the end of the string
) # end of look-ahead
It allows any characters on the string but the dollar only once.
Another variant would be to use two look-aheads, like this:
^(?=[^$]*\$[^$]*$)(?=[a-zA-Z0-9$]{10}$).*
Here you can use the .*
to match the remainder of the string since the two conditions are checked by the look-aheads. This approach is useful for password complexity checks, for example.