Javascript-enabled to display in a span also just insert that line
document.getElementById('valor_span').innerHTML = "R$ "+preco_total;
changing the value of id
on the tag span
for valor_span
for id=valor
has already been assigned to the input
function calcular(el) {
var valor = document.getElementById('valor');
var precoPorLetra = 10;
var espacos = (el.value.split(" ").length - 1); //quantidade de espacos
var caracteres = el.value.length;
preco_total = valor.value = parseFloat( (caracteres - espacos) * precoPorLetra).toFixed(2); //duas casas decimais
document.getElementById('valor_span').innerHTML = "R$ "+preco_total;
}
Nome: <input onkeyup="calcular(this)" type="text" />
Valor: <input id="valor" type="text" readonly />
<span id="valor_span"></span>
With jquery
- Accepts only letters, including accents (not to inflate prices with commas, dots, special characters and etc :D )
- Thousand separator
.
(point) and decimal ,
(comma)
- Spaces are accepted but not counted
$(document).ready(function() {
$('.soLetras').keyup(function (el) {
var precoPorLetra = 10;
$(this).val($(this).val().replace(/[^a-zA-Zà-úÀ-Ú ]/g,''));
var nome = document.getElementById('nome');
var espacos = (nome.value.split(" ").length - 1);
var letras = nome.value.length;
var total = parseFloat((letras-espacos) * precoPorLetra).toFixed(2);
total=total.replace(".", ",");
total=total.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
$('.valor').html("R$ " + total);
valor.value = total;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Nome: <input type="text" class="soLetras" name="nome" id="nome">
Valor: <input id="valor" type="text" readonly />
<span class="valor"></span>
To accept numbers also replace that line
$(this).val($(this).val().replace(/[^a-zA-Zà-úÀ-Ú ]/g,''));
along that line
$(this).val($(this).val().replace(/[^a-zA-Zà-úÀ-Ú0-9 ]/g,''));
PHP
The value of precoPorLetra
from the bank can be assigned in the script as follows:
var precoPorLetra = <?php echo $nomeVariavel ?>;
If you want to take the price value per mysql letter, you will have to make an ajax request.
– Max Rogério
just via ajax? And how would I do that?
– Betinho Silva
You can do either by ajax, or take the value of PHP variable in JS. Example ajax request: http://vinteum.com/ajax-facil-com-jquery/ or pass php variable to JS: https://stackoverflow.com/a/415888/7168666
– Max Rogério
The second option gave it right... but now as I do to display beyond the input in a span tbm
– Betinho Silva
Add the code of
span
in the question and if there is something in the JS that refers to thespan
, put it too. But to give a north to you, creates aspan
and puts an id on it, and blanks print the variable valueprecoPorLetra
, in that span, via ID.– Max Rogério
See if it helps
– Betinho Silva
There cannot be two equal Ids. Change the SPAN ID to "full value" and add the line in the function
document.getElementById('valor_total').innerHTML = "R$"+preco_final;
. And altervalor.value
forpreco_final
.– Sam
Price per letter? dot, colon, question mark, dash, parentheses etccc counts as letter?
– user60252
I think Leo’s answer was more than complete, validate it as correct.
– Max Rogério
@Leocaracciolo yes Leo... count tbm
– Betinho Silva