Función de Validaciones en VB.NET: Control de Errores de Ingreso y Modificación
Función de validaciones en VB.NET
A continuación se presenta una función en Visual Basic .NET que realiza la validación de los campos de un formulario, diferenciando entre el proceso de ingreso y el de modificación, ya que los nombres de los controles cambian según la operación.
Código fuente
Private Function validaciones(ByVal Tipo As String) As Boolean
Dim Msg As String
Dim valida As Boolean = True
Msg = "Errores de Ingreso" & vbCrLf & "****************"
' Selección que permite saber si debe validar al ingresar o al modificar,
' ya que cambian los nombres de los campos.
Select Case Tipo
' SI debe validar al ingresar
Case "INGRESA"
If (txtRut_I.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Rut"
valida = False
End If
If (txtDv_I.Text.Trim.Length > 0) Then
Select Case txtDv_I.Text
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
Case "K", "k"
Case Else
Msg = Msg & vbCrLf & "Debe Ingresar un Digito Verificador Valido"
valida = False
End Select
Else
Msg = Msg & vbCrLf & "Debe Ingresar el Digito Verificador"
valida = False
End If
If (txtNombre_I.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Nombre"
valida = False
End If
If (txtSueldo_I.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Sueldo del Jugador"
valida = False
Else
If ((Val(txtSueldo_I.Text.Trim) < 50000) Or (Val(txtSueldo_I.Text.Trim) > 2000000)) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Valor entre $50.000 y $2.000.000"
valida = False
End If
End If
If (cbbPuesto_I.SelectedIndex <= 0) Then
Msg = Msg & vbCrLf & "Debe Seleccionar el Puesto"
valida = False
End If
' SI debe validar al modificar
Case "MODIFICA"
If (txtRut_M.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Rut"
valida = False
End If
If (txtDv_M.Text.Trim.Length > 0) Then
Select Case txtDv_M.Text
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
Case "K", "k"
Case Else
Msg = Msg & vbCrLf & "Debe Ingresar un Digito Verificador Valido"
valida = False
End Select
Else
Msg = Msg & vbCrLf & "Debe Ingresar el Digito Verificador"
valida = False
End If
If (txtNombre_M.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Nombre"
valida = False
End If
If (txtSueldo_M.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Sueldo del Jugador"
valida = False
Else
If ((Val(txtSueldo_M.Text.Trim) < 50000) Or (Val(txtSueldo_M.Text.Trim) > 2000000)) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Valor entre $50.000 y $2.000.000"
valida = False
End If
End If
If (cbbPuesto_M.SelectedIndex <= 0) Then
Msg = Msg & vbCrLf & "Debe Seleccionar el Puesto"
valida = False
End If
Case "BUSCAR1"
If (txtValorIni.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Valor Inicial"
valida = False
End If
If (txtValorFin.Text.Trim.Length = 0) Then
Msg = Msg & vbCrLf & "Debe Ingresar el Valor Final"
valida = False
End If
Case "BUSCAR2"
If (cbbPuesto_C.SelectedIndex <= 0) Then
Msg = Msg & vbCrLf & "Debe Seleccionar el Puesto"
valida = False
End If
End Select
' Verifica si algún campo no cumplió con la validación,
' de ser así muestra un mensaje con los campos faltantes
If Not valida Then
MsgBox(Msg, MsgBoxStyle.Critical, "AVISO")
Return True
Else
Return False
End If
End Function