java--String类操作:注册系统:验证输入的身份证、手机号、座机号是否合法

本文介绍了一个简单的注册系统验证逻辑实现,包括身份证、手机号及座机号的合法性校验。通过Java代码示例展示了如何进行基本的格式检查。

import java.util.Scanner;

/**
 * 注册系统:验证输入的身份证、手机号、座机号是否合法
 * @author Administrator
 *
 */
public class Register2_Re{
	
	/**
	 * 验证身份证
	 * @param id 传进来的id
	 * @return true 身份证合法, false 身份证不合法
	 */
	public boolean checkId(String id){
		if (id == null) {
			return false;
		}
		if (id.length() != 16 && id.length() != 18) {
			return false;
		}
		return true;
	}
	
	/**
	 * 验证手机号
	 * @param cellphone 传进来的手机号
	 * @return true 手机号合法, false 手机号不合法
	 */
	public boolean checkCellPhone(String cellphone){
		if (cellphone == null) {
			return false;
		}
		if (cellphone.length() != 11) {
			return false;
		}
		return true;
	}
	
	/**
	 * 验证座机号
	 * @param telephone 传进来的座机号
	 * @return true 座机号合法, false 座机号不合法
	 */
	public boolean checkTelephone(String telephone){
		if (telephone == null) {
			return false;
		}
		String[] strs = telephone.split("-");
		if (strs.length != 2 ||
				(strs[0].length() != 4 && strs[1].length() != 7)){
			return false;
		}
		return true;
		
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Register2_Re reg = new Register2_Re();
		
		System.out.println("*******************欢迎进入注册系统*******************");
		System.out.print("\n");
		
		do {
			System.out.println("请输入身份证号码:");
			String id = scanner.next();
			
			System.out.println("请输入手机号:");
			String cellphone = scanner.next();
			
			System.out.println("请输入座机号:");
			String telephone = scanner.next();
			
			boolean flag1 = reg.checkId(id);
			boolean flag2 = reg.checkCellPhone(cellphone);
			boolean flag3 = reg.checkTelephone(telephone);
			
			if (!flag1) {
				System.out.println("身份证号必须是16位或18位");
				continue;
			}
			if (!flag2) {
				System.out.println("手机号必须是11位");
				continue;
			}
			if (!flag3) {
				System.out.println("座机号必须是4位,电话号码必须是7位");
				continue;
			}
			
			if (flag1 && flag2 && flag3) {
				System.out.println("注册成功!");
				break;
			}
			
		} while (true);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值