Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write validation code in swing but For email field if i enter invalid mail then it cant show message and focus will set to phone no field.another problem is when focus in user name field and press tab the focus will go to password field and when i enter pass then tab it will show pass and confirm password doesnt match.it will go in infinite loop.
message will print infinitely

Java
@Override
public void focusLost(FocusEvent e)
{
    if(e.getSource()==nametxt)
    {
        String nme=nametxt.getText();

            for(int i=0;i<nme.length();i++)
            {
                char c=nme.charAt(i);
                if((Character.isLetter(c)))
                    flag=1;
                else
                    flag=0;
            }
            if(nme.length()>1 && flag==1)
                msg.setText("");
            else
            {
                msg.setForeground(Color.RED);
                msg.setText("Enter only character");
                nametxt.requestFocus();
            }



    }
    if(e.getSource()==emailtxt)
    {
        String mail=emailtxt.getText();
        Pattern p =Pattern.compile("^[_a-z0-9]+(\\.[_a-z0-9-]+)*@[a-z0-9]+(\\.[a-z0-9]+)*(\\.[a-z]{2,})$");
        Matcher m = p.matcher(mail);
        boolean bm = m.matches();
        if(bm==true)
        {
            msg.setText("");
        }
        else
        {
            if(mail.length()>1)
            {
                System.out.println("ky re");


            msg.setForeground(Color.RED);
            msg.setText("Enter Valid E-mail id");
            emailtxt.requestFocus();
            }
        }

    }
    if(e.getSource()==phonetxt)
    {
        String ph=phonetxt.getText();
        for(int i=0;i<ph.length();i++)
        {
            char c=ph.charAt(i);
            if((Character.isDigit(c)))
            {
                flag=1;

            }
            else
            {
                flag=0;
            }
        }
        if(ph.length()==10 && flag==1)
        {
            msg.setText("");

        }
        else
        {
            msg.setForeground(Color.red);
            msg.setText("Enter Valid 10 digit no and character are not allowed");
            phonetxt.requestFocus();
        }

    }
    if(e.getSource()==unametxt)
    {
        String nme=unametxt.getText();
        if(nme.length()<1)
        {
            System.out.println("uname true");
            msg1.setForeground(Color.RED);
            msg1.setText("username must be 6 digit long");
            unametxt.requestFocus();
        }
        else
        {
            System.out.println("uname false");
            msg1.setText("");
        }

    }
    if(e.getSource()==passtxt)
    {
        String pw=passtxt.getText();
        int no=0;
        int cap=0;
        int sym=0;
        int lowe=0;
        int ws=0;
        for(int i=0;i<pw.length();i++)
        {
            char c=pw.charAt(i);
            if(Character.isDigit(c))
            {
                no++;

            }
            else if(Character.isLowerCase(c))
            {
                lowe++;
            }
            else if(Character.isUpperCase(c))
            {
                cap++;
            }
            else if(Character.isWhitespace(c))
            {
                ws++;
            }
            else
            {
                sym++;
            }
        }

        if(!(no>=1 && lowe>=1 && cap>=1 && ws==0 && sym>=1 && pw.length()>=8 ))
        {   System.out.println("password true");
            msg1.setForeground(Color.RED);
            msg1.setText("Password must contain 1 no,1capital and 1 symbol");
            passtxt.requestFocus();
        }
        else
        {   System.out.println("password false");
            msg1.setText("");
        }
    }
    if(e.getSource()==cpasstxt)
    {
        String ps=passtxt.getText();
        String cps=cpasstxt.getText();
        if(ps==cps)
        {
            System.out.println("confirm password true");
            msg1.setText("");
        }
        else
        {   System.out.println("onfirm password false");
            msg1.setForeground(Color.red);
            msg1.setText("password and Confirm Password Doesnt match");

        }
    }
}
Posted
Updated 15-Mar-15 23:07pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900