All pastes #41102 Raw Edit

funciones.js

public text v1 · immutable
#41102 ·published 2006-02-11 04:56 UTC
rendered paste body
// ********************************************************************************
// COMO AGREGAR EL SCRIPT DE FUNCIONES PARA QUE SEAN UTILIZADAS DENTRO DE LAS
// PAGINAS WEB.
//
// <head>
// <script language="JavaScript" src="./funciones.js"></script>
// </head>
// ********************************************************************************

// ********************************************************************************
// funcion para volver atras
// <img src="imagen.jpg" name="imagen" onClick="volver_atras()">
// ********************************************************************************

function volver_atras()
{
	parent.location=history.back();
}

// ********************************************************************************
// funcion para validar el rut ingresado
// <input type="text" name="rut" maxlength="8" size="8" 
// onKeypress="validar_numeros(this.value)">
// <input type="text" name="dv" maxlength="1" size="1" 
// onChange="validar_rut(this.value, rut.value)"> 
// ********************************************************************************

function validar_rut(texto,letra)
{
	var l,i,k,c,suma=0,str="";

	if (texto=="")
	{
		return true
	}

	c="32765432765432"

	for (i=0;i<=texto.length;i++)
    		if (texto.charAt(i)!="." || texto.charAt(i)!="")
        		str=str+texto.charAt(i)
	
	texto=str

	for (j=texto.length;j>0;j--){
		i=texto.substring(j-1,j);
        	p=c.substring(c.length-1,c.length)
        	suma=suma + (p*i)
        	c=c.substring(0,c.length-1)
	}

	i=0;
	suma=11-(suma%11);
	
	if (suma==10)
		ch="K";
	else{
		if (suma==11)
        	suma=0;
    		ch=suma;
	}

	if (ch==letra.toUpperCase())
    		return true;
	else
	    	return false;
}

// ********************************************************************************
// funcion para comparar el RUT ingresado con el valor real del digito verificador
// ********************************************************************************

function compara_dv(texto,letra)
{
	letra.value=letra.value.toUpperCase();
	if (texto.value!="")
	{
		if ((texto.value.length)<4)
		{
	  		alert("R.U.T incorrecto")
	     		texto.value="";
		 	letra.value=""
		 	texto.focus();
		} 
		if (!validar_rut(texto.value,letra.value))
		{
			alert("R.U.T incorrecto")
			texto.value="";
			letra.value=""
			texto.focus();
		}
	}
	else
		letra.value="";
}

// ********************************************************************************
// funcion para validar el email ingresado
// ********************************************************************************

function validar_email(nStr)  
{

      	var len = nStr.length;
      	var  i,j;       
      
	if (nStr.indexOf("@")==-1)
	{       
		alert("El formato del e-mail ingresado no es vlido.");
		return (false);
	}
		
      	j = nStr.indexOf("@");
      	nStr=nStr.substring(j+1,len);
        
      	if (nStr.indexOf(".")==-1 )
	{
		alert("El formato del e-mail ingresado no es vlido.");
                return (false);
	}

	if (nStr.indexOf("@")!=-1 )
	{    
		alert("El formato del e-mail ingresado no es vlido.");
                return (false);
	}
      
	if (nStr.lastIndexOf(".")==nStr.length-1)
	{       
		alert("El formato del e-mail ingresado no es vlido.");
                return (false);
	}
        return (true);
}

// ********************************************************************************
// funcion para retornar el mes por el nmero
// ********************************************************************************

function nombremes(nummes){
	var mes="";
	if (nummes==1) mes="Enero";			
	if (nummes==2) mes="Febrero"
	if (nummes==3) mes="Marzo";			
	if (nummes==4) mes="Abril"
	if (nummes==5) mes="Mayo";			
	if (nummes==6) mes="Junio"
	if (nummes==7) mes="Julio";			
	if (nummes==8) mes="Agosto"
	if (nummes==9) mes="Septiembre";	
	if (nummes==10)mes="Octubre";
	if (nummes==11)mes="Noviembre";		
	if (nummes==12)mes="Diciembre"
	return mes;
}

// ********************************************************************************
// funcion para quitarle los primeros caracteres a un string
// ********************************************************************************

function ltrim(caracter,str)
{
	var car="";
	var j=0;
	
	for(i=0;i<=str.length;i++)
	{
		car=str.charAt(i)
		if (car!=caracter)
			break;
		else
			j=j+1;
	}
	str=str.substring(j,str.length+1)
	return str;
}

// ********************************************************************************
// funcion para quitarle los primeros caracteres a un string
// ********************************************************************************

function rtrim(caracter,str)
{
	var car="";
	var j=0;
	
	for(i=0;i<=str.length;i++)
	{
		car=str.charAt(str.length-i-1);
		if (car!=caracter)
			break;
	
		else
			j=j+1;
		}
	str=str.substring(0,str.length-j);
	return str;
}

// ********************************************************************************
// funcion para validar si el string tiene puntos
// ********************************************************************************

function tiene_puntos(str)
{
	var car="";
	for(i=0;i<=str.length;i++)
	{
		car=str.charAt(i)
		if (car==".")
			return true;
	}
	return false;
}

// ********************************************************************************
// funcion para quitarle puntos a un string del tipo 12.345.678.910
// ********************************************************************************

function quitar_puntos(numero)
{
	var car;
	var str="";
	var temp=ltrim("0",numero);
	numero=temp;
	if (tienePuntos(numero))
	{
		for(i=0;i<=numero.length;i++)
		{
			car=numero.charAt(i);
			if (car!="." && car!="")
				str=str+car;
		}
		numero=str;
	}
	return numero;
}

// ********************************************************************************
// funcion para ponerle puntos a un string del tipo 12345678910
// ********************************************************************************

function colocar_puntos(numero)
{
	var texto="";
	var str="";
	numero=Ltrim("0",numero)
	if (!tienePuntos(numero))
	{
		texto = "" + numero;
		if (texto.length==10)
			str=texto.substring(0,1)+"."+texto.substring(1,4)+"."+texto.substring(4,7)+"."+texto.substring(7,10)
		else	
			if (texto.length==9)
				str=texto.substring(0,3)+"."+texto.substring(3,6)+"."+texto.substring(6,9)
			else
				if (texto.length==8)
					str=texto.substring(0,2)+"."+texto.substring(2,5)+"."+texto.substring(5,8)
				else
					if (texto.length==7)
						str=texto.substring(0,1)+"."+texto.substring(1,4)+"."+texto.substring(4,7)
					else
						if (texto.length==6)
							str=texto.substring(0,3)+"."+texto.substring(3,6)
						else
							if (texto.length==5)
								str=texto.substring(0,2)+"."+texto.substring(2,5)
							else
								if (texto.length==4)
									str=texto.substring(0,1)+"."+texto.substring(1,4)
								else
								{
									str=texto
								}
		return(str);
	}
	else
		return(numero)
}

// ********************************************************************************
// funcion para validar solamente el ingreso de numeros en un formulario
// uso : <input type="text" name="texto" onKeyPress="validar_numeros(this,event)">
// ********************************************************************************

function validar_numeros(fld,e)
{	
	var i = j = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) 
		return true;
	if (whichCode == 8) 
		return true;
	if (whichCode == 9) 
		return true;
	
	key = String.fromCharCode(whichCode); 
	
	if (strCheck.indexOf(key) == -1) 
		return false;
		
	len = fld.value.length;
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
}

// ********************************************************************************
// funcion para validar solamente el ingreso de digito verificador en un formulario
// ********************************************************************************

function validar_dv(fld,e)
{	
	var i = j = 0;
	var strCheck = '0123456789Kk';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) 
		return true;
	if (whichCode == 8) 
		return true;
	if (whichCode == 9)
		return true;
	
	key = String.fromCharCode(whichCode); 
	
	if (strCheck.indexOf(key) == -1) 
		return false;
		
	len = fld.value.length;
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
}

// ********************************************************************************
// funcion para validar solamente el ingreso de nombres en un formulario
// ********************************************************************************


function validar_nombres(fld,e)
{	
	var i = j = 0;
	var strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&";
	strCheck = strCheck +"";
	strCheck = strCheck +"' ";
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) 
	    return true;
	if (whichCode == 8) 
	    return true;
	if (whichCode == 9) 
	    return true;
	
	key = String.fromCharCode(whichCode); 
		
	if (strCheck.indexOf(key) == -1) 
		return false;
	
	len = fld.value.length;
		for(; i < len; i++)
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
				aux += fld.value.charAt(i);
}

// ********************************************************************************
// funcion para validar solamente el ingreso de caracteres validos en passwords
// ********************************************************************************


function validar_password(fld,e)
{
        var i = j = 0;
        var strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        strCheck = strCheck +"1234567890";
        var aux = aux2 = '';
        var whichCode = (window.Event) ? e.which : e.keyCode;

        if (whichCode == 13)
            return true;
        if (whichCode == 8)
            return true;
        if (whichCode == 9)
            return true;

        key = String.fromCharCode(whichCode);

        if (strCheck.indexOf(key) == -1)
                return false;

        len = fld.value.length;
        
	for(; i < len; i++)
        {
		if (strCheck.indexOf(fld.value.charAt(i))!=-1)
                {
			aux += fld.value.charAt(i);
		}
	}
}





// ********************************************************************************
// funcion para eliminar espacios en blanco en un formulario
// ********************************************************************************

function eliminar_blancos_al_inicio(s)
{
	var i = 0;
	var xx = 0;
	while ((i < s.length) && (s.charAt(i) == " "))
		i++;
	return s.substring (i, s.length);
}

// ********************************************************************************
// funcion para comparar strings validos de los que no lo son
// ********************************************************************************

function strings_no_validos(nStr)
{
	nStr=blancos(nStr);
	nStr=Rtrim(" ",nStr);
	var i=0;
	var charnoValidos = new Array();
	charnoValidos=["","","","","","","","","","","","","","","","","(",")","<",">","/","|","?","","" ,"'" ,"" ,"!" ,"" ,"," ,";" ,":",'"'," "];
	
	for(i=0;i<=33;i++)
	{
		if (nStr.indexOf(charnoValidos[i])!=-1)
		{
			return false;
		}
	}
	
	var len = nStr.length;
	
	var  i,j;       
	
	if (nStr.indexOf("@")==-1)
                {
                        return (false);
                }
	
	j = nStr.indexOf("@");
	nStr=nStr.substring(j+1,len);
	
	if (nStr.indexOf(".")==-1 )
                {
                        return (false);
                }
	
	if (nStr.indexOf("@")!=-1 )
                {
                        return (false);
                }
      
	if (nStr.lastIndexOf(".")==nStr.length-1)
                {
                        return (false);
                }
        return (true);
}

// ********************************************************************************
// funcion para hacer submit a las paginas web
// ********************************************************************************

function recarga(url,formulario)
{
        str1='window.document.'+formulario+'.action='+'"'+url+'"';
        str2='window.document.'+formulario+'.submit()';
        eval(str1);
        eval(str2);
}