zoukankan      html  css  js  c++  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        

              //方法三

           String regex="^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,15}$";
               System.out.println(passwd.matches(regex));

    63     }
    64 }
  • 相关阅读:
    11->centos6 安装oracle
    centos7安装rlwrap
    ajax
    java获取时间戳
    idea主要设置大纲图
    ppycharm设置解释器版本号码
    JTA 深度历险
    学会数据库读写分离、分表分库——用Mycat,这一篇就够了!
    分库分表的几种常见玩法及如何解决跨库查询等问题
    深入浅出SOA
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/5909346.html
Copyright © 2011-2022 走看看