Jump to content
t0ze

[Resolvido] Validar valor decimal

Recommended Posts

t0ze

Boa tarde pessoal,

É com certeza burrice minha, mas não sei simplesmente o que fazer:

http://jsfiddle.net/SnakeEyes/eu7P9/3/

Não consigo criar um ficheiro .html com esse exemplo.

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">

$('.numbersOnly').keyup(function () {
if(($(this).val().split(".")[0]).indexOf("00")>-1){
 $(this).val($(this).val().replace("00","0"));
} else {
 $(this).val($(this).val().replace(/[^0-9\.]/g,''));
}
});​
</script>
<input type="text" id="numbersOnly" value=""/>​

Alguem pode dar uma dica?

Cumps

Share this post


Link to post
Share on other sites
KiNgPiTo

1º O Selector está a procurar pela class. Em vez de $('.numbersOnly') usa $('#numbersOnly')

2º Cria o corpo do html (head, body)

3º Insere os scripts no head (cria o $(document).ready() e mete a tua função lá...

O tutorial do yoda ajuda-te com isto...

  • Vote 1

Share this post


Link to post
Share on other sites
t0ze

<html>
<head>
 <title></title>
 <script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>
 <script type="text/javascript">
  $(document).ready(function() {
 $('.numbersOnly').keyup(function (e) {
    if(($(this).val().split(".")[0]).indexOf("00")>-1){
	    $(this).val($(this).val().replace("00","0"));
    } else {
	    $(this).val($(this).val().replace(/[^0-9\.]/g,''));
    }
    if($(this).val().split(".")[2] != null || ($(this).val().split(".")[2]).length ){
	    $(this).val($(this).val().substring(0, $(this).val().lastIndexOf(".")));
 }
 });
 });
 </script>
</head>
<body>
 <input type="text" class="numbersOnly" value="" />​
</body>
</html>

Obrigado KiNgPiTo e yoda.

KiNgPiTo se não fosse o teu post a encaminhar-me era difícil, só com o tutorial que o yoda enviou (e que ajudou e q foi mto útil na definição da função)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.