Thanks!

Rick

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Web Specialist
Sent: Sunday, March 11, 2007 4:16 PM
To: jQuery Discussion.
Subject: Re: [jQuery] Adding Currency Validation to Jorn's Validation
Plug-In

Hi Rick. I'm using Jorn's Validation plugin with CF 7.0. But for currency
fields used this example for brazilian values(251.251,84):
<input 
                                                                           
    id="conta__coj_vr_capital_social" 
                                                                           
    name="conta__coj_vr_capital_social" 
                                                                           
    type="text" 
                                                                           
    size="50" 
                                                                           
    value="#LsCurrencyFormat(coj_vr_capital_social,'none')#" 
                                                                           
    onkeypress="return (
String.fromCharCode(window.event.keyCode).search(/[0-9]/i) > -1 );"
                                                                           
    onKeydown="FormataMoeda(this,16,event,2)" 
                                                                           
    maxlength="16" />


onkeypress allows user to insert only numbers and onkeydown formats the
field inserting points and comma. 

FormataMoeda script:

// funcoes relacionadas a formatacao de campo MOEDA/VALOR/MONEY
function Limpar(valor, validos) {
    // retira caracteres invalidos da string
    var result = "";
    var aux;
    for (var i=0; i < valor.length; i++) {
        aux = validos.indexOf(valor.substring(i, i+1));
        if (aux>=0) {
            result += aux;
        }
    }
    return result; 
}

//Formata número tipo moeda usando o evento onKeyDown
function FormataMoeda(campo,tammax,teclapres,decimal) {
    var tecla = teclapres.keyCode;
    vr = Limpar(campo.value,"0123456789");
    tam = vr.length;
    dec=decimal
    
    if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
    
    if (tecla == 8 ){ tam = tam - 1 ; }
    
    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <=
105 ) 
    {
    
        if ( tam <= dec ){ campo.value = vr ; }
    
        if ( (tam > dec) && (tam <= 5) ){
            campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam -
dec, tam ) ; 
        }
        if ( (tam >= 6) && (tam <= 8) ){
            campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam -
5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
        } 
        if ( (tam >= 9) && (tam <= 11) ){
            campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam -
8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr ( tam - dec, tam )
; 
        }
        if ( (tam >= 12) && (tam <= 14) ){
            campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam -
11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) +
"," + vr.substr( tam - dec, tam ) ; 
        }
        if ( (tam >= 15) && (tam <= 17) ){
            campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr ( tam -
14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) +
"." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
        }
    }
    
}

//funcao retorna somente numeros para uma string
function soNumeros(n) 
{
    var s = String(n);
    var n2 = String();
    for(var i = 0; i < s.length; i++) {
        var ss = s.substr(i,1); 
        if(ss >= '0' && ss <= '9') { n2 = n2 + ss; }
        }
    while (n2.length>1 && n2.charAt(0) == "0") n2=n2.substr(1,n2.length);
    return n2;
}


It's a suggestion.

Cheers


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to