Java用户注册格式查错

代码实现Java Swing下用户输入文本框后格式查错
满足要求:
1.密码,姓名,地址必须输入,地址和电话至少输入一个。
2.密码在8-16位之间,姓名为字符串,电话11位,地址包含@和. 。
格式正确后跳转页面并将所有信息写入txt文件

	public void createUser(){
		String contents;
		String userName;		
		
		try {
			FileWriter fileWriter = new FileWriter("user.txt",true);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			
			//judge if the input is valid
			if("".equals(textWord.getText())==false && "".equals(FirstName.getText())==false && "".equals(SurName.getText())==false) {  //password,FirstName,SurName are all input
				if("".equals(Address.getText())==false  || "".equals(TelNum.getText())==false) {//address or telephone are input
					if(7<textWord.getText().length() && textWord.getText().length()<17) {//passwords are between 8 and 16
						if(Address.getText().contains("@") && Address.getText().contains(".")) {
							if(TelNum.getText().length() ==11 && TelNum.getText().matches("\\d{11}")) {
								if(FirstName.getText().matches("[a-zA-Z]+")==true && SurName.getText().matches("[a-zA-Z]+")==true) {
							//change the page
					controlVariable = 1;
					JOptionPane.showMessageDialog(null, "You have been our membership!");
					
					//generate and print the membership number
					 StringBuilder str=new StringBuilder();
					 for(int i=0;i<8;i++){
					 	str.append(new Random().nextInt(10)); 
					 }
					 String num=str.toString();
					 System.out.println("Your membership number is:  "+num);
					 
					 System.out.println(textWord.getText());
					 
					 //write in user.txt
					 contents = num+" " +textWord.getText()+" " +"0 "+FirstName.getText()+" " +SurName.getText()+" " +Address.getText()+" " +TelNum.getText();
						bufferedWriter.write(contents);
						bufferedWriter.write("\r\n");
						bufferedWriter.close();
						fileWriter.close();
						dispose();
							}
								else {//firstname or surname are invalid
									JOptionPane.showMessageDialog(null, "Your firstname or surname are invalid");
								}
							}
							else {//telephone number are invalid
								JOptionPane.showMessageDialog(null, "Your telephone number are invalid");
							}
						
						}
						else {//address are invalid
							JOptionPane.showMessageDialog(null, "Your address are invalid");
						}
					}
					else {//password are not between 8 and 16
						JOptionPane.showMessageDialog(null, "Your passwords must be between 8 and 16");
					}
					
				}
				else {//no address or telephone
					JOptionPane.showMessageDialog(null, "Address or Telephone must be filled out ");
				}
			}
			else {//no password/FirstName/SurName
				JOptionPane.showMessageDialog(null, "Your password/FirstName/SurName must be filled out ");
			}
			
			}catch (IOException e) {
				System.out.println("Errors occured");
				System.exit(1);
			}
	
		
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值