本文介绍了验证码问题。有时它会变成无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在摇摆中编写验证码但是对于电子邮件字段,如果我输入无效邮件,则它无法显示消息,焦点将设置为无电话字段。另一个问题是当焦点在用户名字段中并按Tab键时焦点将转到密码字段,当我输入传递然后选项卡它将显示传递和确认密码不匹配。将进入无限循环。 消息将无限打印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@Overridepublic 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"); } }}推荐答案 这篇关于验证码问题。有时它会变成无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 21:30