Foros del Web » Programación para mayores de 30 ;) » .NET »

Validar rut....

Estas en el tema de Validar rut.... en el foro de .NET en Foros del Web. Hola a todos......saben necesito un poco de ayuda..... Tengo una pagina y un form, en el cual hay un textbox, que se llama rut.....y necesito ...
  #1 (permalink)  
Antiguo 27/10/2003, 13:54
Avatar de vamp_02  
Fecha de Ingreso: febrero-2002
Ubicación: Santiago de Chile
Mensajes: 367
Antigüedad: 22 años, 2 meses
Puntos: 0
Validar rut....

Hola a todos......saben necesito un poco de ayuda.....

Tengo una pagina y un form, en el cual hay un textbox, que se llama rut.....y necesito validarlo.....algun compatriota, de alma caritativa, tendra el script, para validarlo...


Gracias

__________________
yerba mala nunca....te la fumes...
  #2 (permalink)  
Antiguo 27/10/2003, 14:14
Avatar de RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 2 meses
Puntos: 50
Claro que te podemos ayudar..pero que quieres validar... una fecha... una cantidad... un rango... etc..

Se quieres detallanos un poco mas...

Saludos
__________________
Nadie roba nada ya que en la vida todo se paga . . .

Exentrit - Soluciones SharePoint & Net
  #3 (permalink)  
Antiguo 27/10/2003, 15:04
Avatar de vamp_02  
Fecha de Ingreso: febrero-2002
Ubicación: Santiago de Chile
Mensajes: 367
Antigüedad: 22 años, 2 meses
Puntos: 0
Para explicar soy malo...pero este script, me validaba el rut de aca de chile....pero esta en asp, y lo quiero instalar en una pagina asp.net

<script language='VBScript'>
Dim V_Mensaje

Function StrToCifra(ByVal Str)
Dim Str_Aux
V_Indice1 = Len(Str)
V_Indice2 = 0
Str_Aux=""

While V_Indice1 > 0
if (V_Indice2 = 3 and V_Indice1 >= 1) Then
Str_Aux = Str_Aux & "."
V_Indice2 = 0
End If
Str_Aux = Str_Aux & Mid(Str,V_Indice1,1)
V_Indice1 = V_Indice1 - 1
V_Indice2 = V_Indice2 + 1
WEnd

V_Indice1 = Len(Str_Aux)
Str=""
While V_Indice1 > 0
Str = Str & Mid(Str_Aux,V_Indice1,1)
V_Indice1 = V_Indice1 - 1
WEnd
StrToCifra = Str
End Function

Function Remove_Char(Str,Ch)
V_Indice1 = Len(Str)
V_Indice2 = 1

While V_Indice2 <= V_Indice1
if Ch <> Mid(Str,V_Indice2,1) Then
Str_Aux = Str_Aux & Mid(Str,V_Indice2,1)
End If
V_Indice2 = V_Indice2 + 1
WEnd
Remove_Char = Str_Aux
End Function

Function Valida_Rut(Texto)
Dim RUT_Len
Dim ACUM

set Formulario=document.Form_Acceso
RUT = Remove_Char(Ucase(Texto.value),".")
RUT = Remove_Char(RUT," ")
RUT_Len = Len(RUT)
If RUT_Len = 0 Then
Msgbox "Debe Ingresar RUT.",48,"Error"
Valida_Rut = false
Exit Function
End If
RUT_DV = Mid(RUT, RUT_Len, RUT_Len)
if RUT_DV = "" Then
Msgbox "Ingrese RUT con su digito verificador",48,"Error"
Valida_Rut = false
exit Function
End If
pos_guion = InStr(RUT,"-")
if pos_guion=0 Then
pos_guion = RUT_Len
End If
RUT_RUT = "000000000000000" & Mid(RUT, 1, pos_guion-1)
if (RUT_RUT="") Then
Msgbox "Debe Ingresar RUT valido.",48,"Error"
Valida_Rut = false
exit Function
End If
if not IsNumeric(RUT_RUT) then
Msgbox "El RUT ingresado posee caracteres no v&aacute;lidos",48,"Error"
Valida_Rut = false
exit Function
end if
RUT_RUT_Len = Len(RUT_RUT)
ACUM = 0
Factor = 2
While RUT_RUT_Len > 0
if Factor > 7 Then
Factor=2
End If
ACUM = ACUM + Factor * Mid(RUT_RUT, RUT_RUT_Len,1)
RUT_RUT_Len = RUT_RUT_Len-1
Factor = Factor+1
WEnd
G = ACUM Mod 11
h = 11 - G
Select Case h
Case 10
veri = "K"
Case 11
veri = "0"
Case else
veri = h
End Select
If Trim(veri) <> Trim(RUT_DV) Then
Msgbox "El Dígito Verificador ingresado no corresponde al RUT.",48,"Error"
Valida_Rut = false
Exit Function
End If
Texto.value = right(StrToCifra(RUT_RUT) & "-" & RUT_DV,12)
Valida_Rut = true
End Function
</script>


Y la llamada a la funcion, sería:

<input type="text"
name=V_Form_Rut
size="14"
maxlength="13"
onchange="Valida_Rut(document.Form_Acceso.V_Form_R ut)">
__________________
yerba mala nunca....te la fumes...
  #4 (permalink)  
Antiguo 12/11/2003, 18:06
Avatar de RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 2 meses
Puntos: 50
Mira.. siguiendo la lógica que tienes en tu VBscript, para net es casi lo mismo...lo que hice fue tener 1 cuadro de texto (txtRut), un boton y una etiqueta (label1) donde me desplegará cualquier tipo de error.

Y el código sería de ésta manera.
Cita:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Valida_Rut(txtRut.Text)
End Sub

y las funciones:

Cita:
Function StrToCifra(ByVal str As String) As String
Dim str_Aux As String
Dim V_Indice1, V_Indice2 As Integer

V_Indice1 = Len(str)
V_Indice2 = 0
str_Aux = ""

While V_Indice1 > 0
If (V_Indice2 = 3 And V_Indice1 >= 1) Then
str_Aux = str_Aux & "."
V_Indice2 = 0
End If
str_Aux = str_Aux & Mid(str, V_Indice1, 1)
V_Indice1 = V_Indice1 - 1
V_Indice2 = V_Indice2 + 1
End While

V_Indice1 = Len(str_Aux)
str = ""
While V_Indice1 > 0
str = str & Mid(str_Aux, V_Indice1, 1)
V_Indice1 = V_Indice1 - 1
End While

Return str

End Function

Function Remove_Char(ByVal Str As String, ByVal Ch As String) As String
Dim str_Aux As String
Dim V_Indice1, V_Indice2 As Integer

V_Indice1 = Len(Str)
V_Indice2 = 1

While V_Indice2 <= V_Indice1
If Ch <> Mid(Str, V_Indice2, 1) Then
str_Aux = str_Aux & Mid(Str, V_Indice2, 1)
End If
V_Indice2 = V_Indice2 + 1
End While

Return str_Aux

End Function

Function Valida_Rut(ByVal Texto As String)
Dim RUT_Len As Integer
Dim rut As String
Dim ACUM

Dim Formulario As String = txtRut.Text
rut = Remove_Char(UCase(Texto), ".")
rut = Remove_Char(rut, " ")
RUT_Len = Len(rut)

If RUT_Len = 0 Then
MsgBox("Debe Ingresar RUT.", 48, "Error")
Valida_Rut = False
Exit Function
End If

Dim RUT_DV As String = Mid(rut, RUT_Len, RUT_Len)

If RUT_DV = "" Then
Label1.Text = "Ingrese RUT con su digito verificador"
Valida_Rut = False
Exit Function
End If

Dim pos_guion As Integer = InStr(rut, "-")
If pos_guion = 0 Then
pos_guion = RUT_Len
End If

Dim RUT_RUT As String = "000000000000000" & Mid(rut, 1, pos_guion - 1)

If (RUT_RUT = "") Then
Label1.Text = "Debe Ingresar RUT valido."
Valida_Rut = False
Exit Function
End If

If Not IsNumeric(RUT_RUT) Then
Label1.Text = "El RUT ingresado posee caracteres no válidos"
Valida_Rut = False
Exit Function
End If
Dim RUT_RUT_Len As Integer = Len(RUT_RUT)
ACUM = 0
Dim Factor As Integer = 2

While RUT_RUT_Len > 0
If Factor > 7 Then
Factor = 2
End If
ACUM = ACUM + Factor * Mid(RUT_RUT, RUT_RUT_Len, 1)
RUT_RUT_Len = RUT_RUT_Len - 1
Factor = Factor + 1
End While
Dim G As Integer = ACUM Mod 11
Dim h As Integer = 11 - G

Dim veri As String

Select Case h
Case 10
veri = "K"
Case 11
veri = "0"
Case Else
veri = h
End Select
If Trim(veri) <> Trim(RUT_DV) Then
Label1.Text = "El Dígito Verificador ingresado no corresponde al RUT."
Valida_Rut = False
Exit Function
End If
txtResultado.Text = Right(StrToCifra(RUT_RUT) & "-" & RUT_DV, 12)
Valida_Rut = True
End Function
Si te das cuenta casi todo se queda de la misma manera.. pero sería cosa que lo checaras ok..??

Saludos y suerte
  #5 (permalink)  
Antiguo 15/12/2010, 19:32
 
Fecha de Ingreso: noviembre-2010
Mensajes: 49
Antigüedad: 13 años, 5 meses
Puntos: 0
Respuesta: Validar rut....

graxias por casualidad llegue aka y me sirvio bastante ya que habia posteado una pregunta parecida
  #6 (permalink)  
Antiguo 15/12/2010, 22:21
 
Fecha de Ingreso: enero-2010
Mensajes: 4
Antigüedad: 14 años, 3 meses
Puntos: 0
Respuesta: Validar rut....

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Página sin título</title>
</head>
<script type="text/javascript" language=javascript>
function revisarDigito( crut, ctexto2 )
{
largo = crut.length;
rut = crut.substring(0, largo - 1);
dv = crut.charAt(largo-1);
var dvr = '0';
suma = 0;
mul = 2;
for (i= rut.length -1 ; i >= 0; i--)
{
suma = suma + rut.charAt(i) * mul
if (mul == 7)
mul = 2;
else
mul++;
}
if (suma > 0)
{
res = suma % 11;
if (res==1)
dvr = 'k';
else if (res==0)
dvr = '0';
else
{
dvi = 11-res;
dvr = dvi + "";
}
if ( dvr != dv.toLowerCase() )
{
alert("EL rut es incorrecto");
document.getElementById(ctexto2).value = "";
window.document.getElementById(ctexto2).focus();
return false;
}
return true;
}else
{
alert("EL rut es incorrecto");
return false;
}
}

function Rut(rut)
{
var texto;
texto = document.getElementById(rut).value;
var texto2;
texto2 = rut;
var tmpstr = "";
for ( i=0; i < texto.length ; i++ )
{
if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
{
tmpstr = tmpstr + texto.charAt(i);
}
}
texto = tmpstr;
largo = texto.length;
if ( largo > 7 )
{
for (i=0; i < largo ; i++ )
{
if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" )
{
alert("El valor ingresado no corresponde a un R.U.T valido");
document.getElementById(texto2).value = "";
window.document.getElementById(texto2).focus();
return false;
}
}
var invertido = "";
for ( i=(largo-1),j=0; i>=0; i--,j++ )
{
invertido = invertido + texto.charAt(i);
}
var dtexto = "";
dtexto = dtexto + invertido.charAt(0);
dtexto = dtexto + '-';
cnt = 0;
for ( i=1,j=2; i<largo; i++,j++ )
{
if ( cnt == 3 )
{
dtexto = dtexto + '.';
j++;
dtexto = dtexto + invertido.charAt(i);
cnt = 1;
}
else
{
dtexto = dtexto + invertido.charAt(i);
cnt++;
}
}
invertido = "";
for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
{
invertido = invertido + dtexto.charAt(i);
document.getElementById('textRut').value = invertido.toUpperCase();
}
if ( revisarDigito(texto, texto2) )
{
alert("El Rut es correcto");
return true;
}
}else
{
alert("Debe ingresar el rut completo")
window.document.getElementById(texto2).focus();
return false;
}
return false;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="textRut" runat="server"></asp:TextBox>
<asp:Button ID="btnValidarRut" runat="server" Text="Validar" />
</div>
</form>
</body>
</html>


codebehind

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.btnValidarRut.Attributes.Add("onclick", "javascript:return Rut('" & textRut.ClientID & "')")
End Sub


C#

protected void Page_Load(object sender, EventArgs e)
{
btnValidarRut.Attributes.Add("onclick", "javascript:return Rut('" + textRut.ClientID + "')");
}
  #7 (permalink)  
Antiguo 19/12/2010, 15:04
 
Fecha de Ingreso: noviembre-2010
Mensajes: 49
Antigüedad: 13 años, 5 meses
Puntos: 0
Pregunta Respuesta: Validar rut....

Cita:
Iniciado por RootK Ver Mensaje
Mira.. siguiendo la lógica que tienes en tu VBscript, para net es casi lo mismo...lo que hice fue tener 1 cuadro de texto (txtRut), un boton y una etiqueta (label1) donde me desplegará cualquier tipo de error.

Y el código sería de ésta manera.



y las funciones:



Si te das cuenta casi todo se queda de la misma manera.. pero sería cosa que lo checaras ok..??

Saludos y suerte

hola una consulta como podria hacer esto mismo pero en C# ayuda plis
  #8 (permalink)  
Antiguo 20/12/2010, 09:54
Avatar de Aquaventus  
Fecha de Ingreso: junio-2010
Ubicación: Lima-Peru , En el alba de la naturaleza
Mensajes: 2.105
Antigüedad: 13 años, 10 meses
Puntos: 267
Respuesta: Validar rut....

xikristian usa esta pagina http://www.developerfusion.com/tools/convert/vb-to-csharp/ y no revivas temas viejos por favor, crea un nuevo post para aclarar tus dudas. Probablemente te salga un error en algunas lineas pero ya tu debes corregir. Saludos.
  #9 (permalink)  
Antiguo 20/12/2010, 11:52
 
Fecha de Ingreso: noviembre-2010
Mensajes: 49
Antigüedad: 13 años, 5 meses
Puntos: 0
Respuesta: Validar rut....

ok intentare asi graxias
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 3 personas (incluyéndote)




La zona horaria es GMT -6. Ahora son las 19:30.