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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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使用正则表达式对注册页面进行验证的主要内容,如果未能解决你的问题,请参考以下文章

使用正则表达式在控制器中进行验证

js正则表达式注册页面表单验证

jsp页面中正则表达式的使用

如何用正则表达式验证用户名

正则表达式对手机号的验证^[1][3-8]+\\d9

JQuery使用正则表达式验证手机号,邮箱,身份证(含有港澳台)