Está en la página 1de 3

package VALIDACIONES

import java.awt.Color;

import java.awt.Toolkit;

import javax.swing.JTextField;

/**

* @author BM- */

public class Validación {

public void onlyNumbers(java.awt.event.KeyEvent evt){

int key = (int)evt.getKeyChar();

if(key<48 || key>57){

evt.consume();

Toolkit.getDefaultToolkit().beep();

public void onlyText(java.awt.event.KeyEvent evt){

int key = (int)evt.getKeyChar();

if((key<65 || key>90) && (key<97 || key>122) && key!=209 && key!=241 ){

evt.consume();

Toolkit.getDefaultToolkit().beep();

public void textSize(javax.swing.JTextField txt, int length){

if(txt.getText().length()>=length){

txt.setText(txt.getText().substring(0, length-1));

Validaciones

txt.requestFocus();
Toolkit.getDefaultToolkit().beep();

public boolean vacío(javax.swing.JTextField[] txts){

boolean flag = true;

for (JTextField txt : txts) {

if(txt.getText().trim().length()<2){

flag = false;

txt.setBackground(Color.red);

txt.requestFocus();

//break;

return flag;

}FramePerson

private void txtcédulaKeyTyped(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

new Validación().soloNúmeros(evt);

new Validación().tamañoTexto(txtcédula, 10);

private void txtedadKeyTyped(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

new Validación().onlyNumbers(evt);

new Validación().textSize(txtedad, 2);

Validaciones

private void txtteléfonoKeyTyped(java.awt.event.KeyEvent evt) {


// TODO add your handling code here:

new Validación().onlyNumbers(evt);

new Validación().textSize(txtteléfono, 12);

private void txtnombreKeyTyped(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

new Validación().onlyText(evt);

new Validación().vacío(txts);

new Validación().textSize(txtnombre, 25);

private void txtapellidoKeyTyped(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

new Validación().onlyText(evt);

new Validación().vacío(txts);

new Validación().textSize(txtapellido, 25);

private void txtdireccionKeyTyped(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

new Validación().onlyText(evt);

new Validación().vacío(txts);

new Validación().textSize(txtdirección, 45);

También podría gustarte