请编写一个证验身份证的程序

2025-04-07 01:23:59
推荐回答(4个)
回答1:

struts action中验证(有注释,我自己写的,记得给加分):
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

//获取从登录框中得到的信息
ActionForward forward=new ActionForward();

String markname=mapping.getParameter();
//System.out.println(markname);

LogonForm logonForm = (LogonForm) form;
ActionErrors errors= new ActionErrors();
String username=logonForm.getUsername();
String password=logonForm.getUserpassword();

//System.out.println("测试结果是"+username);

//验证用户没有添加用户名或密码的情况
boolean mark = true;

if(username==null||username.equals(""))
{
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("income.no.username.error"));
mark=false;
}
if(password==null||password.equals(""))
{
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("income.no.password.error"));
mark=false;
}
if(!mark)
{
saveErrors(request,errors);
return mapping.findForward("false");
}//验证结束

//连接数据库开始检查用户是否输入的正确的用户名和密码,正确将logonmark设为true,错误将logonmark设为false
boolean logonmark=true;
DB db=new DB();
//String str1="update userinfo set sex=9 where userid=1";
//int n=db.update(str1);
String str="select * from userinfo where username='"
+username+"' and userpassword='"+password+"'";
ResultSet rs=db.getRs(str);

try{
if(rs.next())
{
logonmark=true;
//return mapping.findForward("success");
//将整个LogForm对象的属性填满,作为持久性类
logonForm.setUserid(rs.getString(Content.TB_USERID));
logonForm.setUsername(rs.getString(Content.TB_USERNAME));
logonForm.setUserpassword(rs.getString(Content.TB_USERPASSWORD));
logonForm.setUserbirthday(rs.getString(Content.TB_USERBIRTHDAY));
logonForm.setUserbumenid(rs.getString(Content.TB_USERBUMENID));
logonForm.setUserform(rs.getString(Content.TB_USERFORM));
logonForm.setUsermailbox(rs.getString(Content.TB_USERMAILBOX));
logonForm.setUsermobilephoneNo(rs.getString(Content.TB_MOBILEPHONENO));
logonForm.setUserphoneNo(rs.getString(Content.TB_PHONENO));
logonForm.setUsersex(rs.getString(Content.TB_USERSEX));
logonForm.setUserruxue(rs.getString(Content.TB_USERRUXUE));
}
else
{
logonmark=false;
}
}catch(SQLException e)
{
e.printStackTrace(System.err);
}
finally
{
db.closed();
}
//设定logonmark操作结束

//根据logonmark标记确定页面转发情况
if(!logonmark)
{
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("income.name.or.password.wrong.error"));
saveErrors(request,errors);
return mapping.findForward("false");
}
else
{
return mapping.findForward("success");
}

}

回答2:

/*
*IDValidator .java
*根据身份证规则进行验证某个身份证号是否为合法证件号.
*/
public class IDValidator {

/**
* 第i位置上的加权因子
*/
public static final int[] WI = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
public static final char[] C = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
/**
* @param args
*/
public static void main(String[] args) {
try {
if(validate("11010519491231002x"))
System.out.println("OK!");
} catch (Exception e) {
System.out.println(e.getMessage());
}

}

public static boolean validate(String id) throws Exception{
int s = 0;//号码与因子WI的加权
int ai = 0;//身份证号的挨个数字值
int Y = 0;
if(id == null || id.length()!=18){
throw new Exception("号码长度不合适!请输入18位身份证!");
}
for(int i = 0; i < 18 - 1; i++){
try {
ai = Integer.parseInt(id.charAt(i)+"");
s += ai * WI[i];
} catch (NumberFormatException e) {
throw new Exception("号码内容不合适!除最后一位可输入X或Y之外,其余请全部输入数字!");
}
}
Y = s%11;
char c = C[Y];
id = id.toUpperCase();
if(c!=id.charAt(17))
throw new Exception("该ID为非法!最后一位应该为:"+c);
return true;
}

}

回答3:

验证身份证,建议用js的正则表达式。正则表达式可以验证很复杂的数据,
关于正则表达式你可以去网上搜索资料

回答4:

什么语言的?