首页 > 代码库 > java使用正则表达式对注册页面进行验证

java使用正则表达式对注册页面进行验证

 1 package regex; 2  3 import java.util.Scanner; 4 import java.util.regex.Matcher; 5 import java.util.regex.Pattern; 6  7 public class registered { 8  9     public static void main(String[] args) {10         //注册用户11         Scanner sc=new Scanner(System.in);12         System.out.println("请输入用户名:");13         String uname=sc.next();14         System.out.println("请输入密码:");15         String passwd=sc.next();16         System.out.println("请输入确认密码:");17         String repasswd=sc.next();18         19 /*        String uname="wangheng";20         String passwd="222assAS123";21         String repasswd="432Pass123";*/        22                 23         boolean b=uname.matches("\\w{3,10}");                    //方法一24         if(b==true){25         Pattern p0=Pattern.compile(".{6,12}");//长度6到12个26         Pattern p1=Pattern.compile(".*[A-Z]+");//27         Pattern p2=Pattern.compile(".*[a-z]+");28         Pattern p3=Pattern.compile(".*\\d+");29         Matcher m0=p0.matcher(passwd);30         Matcher m1=p1.matcher(passwd);31         Matcher m2=p2.matcher(passwd);32         Matcher m3=p3.matcher(passwd);33         if(m0.lookingAt()==true&&34            m1.lookingAt()==true&&        35            m2.lookingAt()==true&&36            m3.lookingAt()==true){37                 boolean b2=passwd.matches(repasswd);38                 if(b2){39                     System.out.println("注册成功!");40                 }else{41                     System.out.println("确认密码与密码不同!");42                 }43         }else{44                 System.out.println("密码输入错误!");45             }46         }else{47             System.out.println("用户名输入错误!");48         }49         50         //方法二51         Pattern p1=Pattern.compile("[A-Z]+");52         Pattern p2=Pattern.compile("[a-z]+");53         Pattern p3=Pattern.compile("\\d+");54         Matcher m1=p1.matcher(passwd);55         Matcher m2=p2.matcher(passwd);56         Matcher m3=p3.matcher(passwd);57         if(uname.matches("\\w{3,10}")&&passwd.matches(".{6,12}")&&m1.find()&&m2.find()&&m3.find()){58             System.out.println("注册成功!");59         }else{60             System.out.println("注册失败!");61         }62         63     }64 }

 

java使用正则表达式对注册页面进行验证