• 添加学生信息(java wb)


    要求: 

    1、登录账号:要求由612位字母、数字、下划线组成,只有字母可以开头;

    2、登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成;

    3、性别:要求用单选框或下拉框实现,选项只有“男”或“女”;

    4、学号:要求八位数字组成,前四位为2018”开头,输入自己学号;

    5、姓名:输入自己的姓名;

    6、电子邮箱:要求判断正确格式xxxx@xxxx.xxxx

    7、点击“添加”按钮,将学生个人信息存储到数据库中;

    8、可以演示连接上数据库。

    一、源代码如下:

    dao.java

    package test;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;

    import test.User;
    import test.DBUtil;;

    public class dao
    {

    //娣诲姞
    //娣诲姞鍏�rade琛ㄩ噷

    public boolean insert(User user)
    {
    String sql="insert into resgist(username,password,sex,name,number,email,clas,xi,banji,address,iphone,beizhu) values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getSex()+"','"+user.getName()+"','"+user.getNumber()+"','"+user.getEmail()+"','"+user.getClas()+"','"+user.getXi()+"','"+user.getBanji()+"','"+user.getAddress()+"','"+user.getIphone()+"','"+user.getBeizhu()+"')";
    //insert璇�彞锛�
    //insert INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
    //娉ㄦ剰锛歩nsert杩欓噷 values(100,'鍚存灄绁�','"+bianliang+"')锛屾暟瀛楀彲浠ョ洿鎺ヤ笂锛屽瓧绗︿覆鐨勮瘽鐢� '',鍙橀噺鐨勮瘽瑕佺敤 '"++"'

    Connection conn=DBUtil.getConn();//鏁版嵁搴撹繛鎺ワ紝鍔犺浇椹卞姩
    Statement state=null;//鏁版嵁搴撴搷浣�
    try
    {
    state=conn.createStatement();//瀹炰緥鍖朣tatement瀵硅薄,鏂逛究瀵箂ql璇�彞杩涜�鎿嶄綔
    state.executeUpdate(sql);
    //鎵ц�鏁版嵁搴撴洿鏂版搷浣滅敤浜庢墽琛孖NSERT銆乁PDATE鎴朌ELETE璇�彞浠ュ強SQLDDL锛堟暟鎹�畾涔夎�瑷�锛夎�鍙ワ紝
    //渚嬪�CREATETABLE鍜孌ROPTABLE,锛堝垱寤鸿〃鍜屽垹闄よ〃锛�
    }catch(Exception e)//褰搕ry璇�彞涓�嚭鐜板紓甯告椂锛屼細鎵ц�catch涓�殑璇�彞
    {
    e.printStackTrace();//鎹曡幏寮傚父鐨勮�鍙�
    }
    finally //finally浣滀负寮傚父澶勭悊鐨勪竴閮ㄥ垎锛屽畠鍙�兘鐢ㄥ湪try/catch璇�彞涓�紝骞朵笖闄勫甫涓�涓��鍙ュ潡锛岃〃绀鸿繖娈佃�鍙ユ渶缁堜竴瀹氫細琚�墽琛岋紙涓嶇�鏈夋病鏈夋姏鍑哄紓甯革級锛岀粡甯歌�鐢ㄥ湪闇�瑕侀噴鏀捐祫婧愮殑鎯呭喌涓嬨��
    {
    DBUtil.close(state, conn); //close閲婃斁璧勬簮
    }
    return false;
    }

    // //鍒犻櫎
    // //鏍规嵁ID鍒犻櫎
    //
    // public boolean delete(int id)
    // {
    // String sql="delete from grade_student where ID='"+id+"'";
    // //鍒犻櫎璇�彞锛�
    // //delete from table_name where some_colume=some_value
    //
    // Connection conn=DBUtil.getConn();
    // Statement state=null;
    //
    // try
    // {
    // state=conn.createStatement();
    // state.executeUpdate(sql);
    // }catch (Exception e)
    // {
    // e.printStackTrace();
    // }
    // finally
    // {
    // DBUtil.close(state, conn);
    // }
    // return false;
    // }
    //
    // //淇�敼
    // //鏍规嵁瀛﹀彿淇�敼
    //
    // public boolean update(Grade grade)
    // {
    // String sql="update grade_student set Chinese='"+grade.getChinese()+"',Math='"+grade.getMath()+"',English='"+grade.getEnglish()+"',SUM='"+grade.getSUM()+"' where ID='"+grade.getID()+"' ";
    // //update璇�彞锛�
    // //update table_name set column1=value1,column2=value2 where some_column=some_value;
    //
    // Connection conn=DBUtil.getConn();
    // Statement state=null;
    //
    // try
    // {
    // state=conn.createStatement();
    // state.executeUpdate(sql);
    // }catch (SQLException e)
    // {
    // e.printStackTrace();
    // }
    // finally
    // {
    // DBUtil.close(state, conn);
    // }
    // return false;
    // }
    //
    // //鏌ヨ�
    // //鏌ヨ�鍏ㄩ儴骞跺瓨鍏�ist
    // //List鎸囩殑鏄�泦鍚�.<>鏄�硾鍨�,閲岄潰鎸囧畾浜嗚繖涓�泦鍚堜腑瀛樻斁鐨勬槸浠�涔堟暟鎹�
    // //娉涘瀷鏄�▼搴忚�璁¤�瑷�鐨勪竴绉嶇壒鎬с��
    // //鍏佽�绋嬪簭鍛樺湪寮虹被鍨嬬▼搴忚�璁¤�瑷�涓�紪鍐欎唬鐮佹椂瀹氫箟涓�浜涘彲鍙橀儴鍒嗭紝
    // //浣嗘槸锛岄偅浜涢儴鍒嗗湪浣跨敤鍓嶅繀椤讳綔鍑烘寚鏄庛��
    //
    // public List<User>list()
    // {
    // String sql="select * from grade_student order by ID ASC";
    // //select璇�彞
    // //select * from tabel_name 杩欐槸鏌ヨ�鎵�鏈夛紝鑻ヨ�鏌ヨ�鏌愪竴鍒�
    // //select column1_name,column2_name,column3_name from table_name
    //
    // List<User>list=new ArrayList<>();
    // //缁欓泦鍚坙ist鍒涢�狅紙new锛変竴涓�瓨鍌ㄧ┖闂寸敤浜庡瓨鏀炬暟鎹�
    //
    // Connection conn=DBUtil.getConn();
    // Statement state=null;
    // ResultSet rs=null;
    //
    // try
    // {
    // state=conn.createStatement();
    // rs=state.executeQuery(sql);
    // User user=null;
    // while(rs.next()) //杩欓噷琛ㄧず锛屽綋rs.next()鏈夊�煎緱鏃跺�欙紝鍗虫敹鍒版煡璇㈢粨鏋�
    // {
    // String id=rs.getString("ID"); //娉ㄦ剰锛氳繖閲岀敤鍙屽紩鍙凤紝ID鏄�〃grade_student閲岀殑ID鍒�
    // String name=rs.getString("NAME");
    // int Chinese=rs.getInt("Chinese");
    // int Math=rs.getInt("Math");
    // int English=rs.getInt("English");
    // int SUM=rs.getInt("SUM");
    // user=new User(username,password,name,,English,SUM);
    // list.add(user); //琛ㄧず锛屾妸bean閲岀殑鏁版嵁瀛樺叆鍒發ist褰撲腑
    // }
    // }
    // catch (SQLException e)
    // {
    // e.printStackTrace();
    // }
    // finally
    // {
    // DBUtil.close(state, conn);
    // }
    // return list;
    // }
    //
    //楠岃瘉娉ㄥ唽鏄�惁鍞�竴
    public boolean username(String username) {

    boolean flag = false;

    String sql = "select username from resgist where username = '" + username + "'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    ResultSet rs = null;

    try {
    state = conn.createStatement();
    rs = state.executeQuery(sql);
    //瑕佺敤statement绫荤殑executeQuery()鏂规硶鏉ヤ笅杈緎elect鎸囦护浠ユ煡璇㈡暟鎹�簱锛�
    //executeQuery()鏂规硶浼氭妸鏁版嵁搴撳搷搴旂殑鏌ヨ�缁撴灉瀛樻斁鍦≧esultSet绫诲�璞′腑渚涙垜浠�娇鐢ㄣ��
    while (rs.next()) {
    flag = true;
    }
    }
    catch (SQLException e) {
    e.printStackTrace();
    } finally {
    DBUtil.close(rs, state, conn);
    }
    return flag;
    }
    }

    DBUtil.java

    package test;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    /**
    * 鏁版嵁搴撹繛鎺ュ伐鍏�
    * @author 鍚存灄绁�
    *
    */
    public class DBUtil {
    //鑱旂粨瀛楃�涓� //鏁版嵁搴撳悕test
    public static String db_url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8";
    //鏁版嵁搴撶敤鎴峰悕
    public static String db_user = "root";
    //鏁版嵁搴撳瘑鐮佸悕
    public static String db_pass = "123";

    public static Connection getConn () {

    //澹版槑涓庢暟鎹�簱鐨勮繛鎺ュ苟瀹炰緥鍖栦负null
    Connection conn = null;

    try {
    //椹卞姩绋嬪簭鍚�
    Class.forName("com.mysql.jdbc.Driver");//杩炴帴鏁版嵁搴�
    //鍏蜂綋鍦拌繛鎺ュ埌鏁版嵁搴撯�斺�旇仈鎺ュ瓧绗︿覆锛堟暟鎹�簱鍚嶏級锛岃仈鎺ョ敤鎴峰悕锛岃仈鎺ュ瘑鐮佸悕
    conn = DriverManager.getConnection(db_url, db_user, db_pass);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return conn;
    }

    /**
    * 鍏抽棴杩炴帴
    * @param state
    * @param conn
    */
    public static void close (Statement state, Connection conn) {
    if (state != null) {
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    public static void close (ResultSet rs, Statement state, Connection conn) {
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (state != null) {
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    }

    service.java

    package test;

    import java.util.List;
    import test.dao;
    import test.User;

    /**
    * CourseService
    * 鏈嶅姟灞�
    * @author Hu
    *
    */

    //service灞傜浉褰撲簬涓�涓�皝瑁呭眰锛屽皝瑁呴偅浜涗笌涓氬姟鐩稿叧鐨勯�氱敤鏁版嵁鎺ュ彛

    public class service
    {
    dao cdao=new dao();
    /**
    * 娣诲姞
    * @param course
    * @return
    */
    public boolean insert(User user)
    {
    boolean f=false;
    if(!cdao.username(user.getUsername()))
    {
    cdao.insert(user);
    f=true;
    }
    return f;
    }

    //鍒犻櫎

    // public void delete(int id)
    // {
    // cdao.delete(id);
    // }
    //
    // //淇�敼
    //
    // public void update (Grade grade)
    // {
    // cdao.update(grade);
    // }
    //
    // //閫氳繃ID寰楀埌涓�涓狢ourse
    //
    // //閫氳繃name寰楀埌涓�涓狢ourse
    //
    // //鏌ユ壘
    //
    //
    // //鍏ㄩ儴鏁版嵁閬嶅巻
    //
    // public List<Grade>list()
    // {
    // return cdao.list();
    // }
    }

    servlet.java

    package test;

    import java.io.IOException;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import test.User;
    import test.service;

    @WebServlet("/servlet")
    public class servlet extends HttpServlet //缁ф壙鐨勬槸 HttpServlet鏂规硶锛屽拰缃戦〉浜や簰鐨勬柟娉�
    {
    private static final long serialVersionUID = 1L;

    service service=new service();

    //鏂规硶閫夋嫨

    protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException
    {
    //req鏄疪equest锛堣�姹傦級鐨勭畝鍐欙紝鎸囩殑鏄�粠jsp/html鐣岄潰璇锋眰鍥炴暟鎹�紝鍗� jsp/html 鈫� 搴曞眰
    //resp鏄疪esponse锛堝洖绛旓級鐨勭畝鍐欙紝鎸囩殑鏄�彂閫佺粰jsp/html鐨勬暟鎹�紝锛屽嵆 搴曞眰 鈫� jsp/html

    req.setCharacterEncoding("utf-8");//璁剧疆浠巎sp鎴朒TML涓��姹傛暟鎹�殑鍊硷紝涔熷氨鏄��缃�负涓�枃锛岄槻姝�贡鐮�
    String method=req.getParameter("method");//getParameter()鑾峰彇鐨勬槸瀹㈡埛绔��缃�殑鏁版嵁銆�
    if ("insert".equals(method)) {
    insert(req, resp);
    }
    // //澧�
    // else if ("delete".equals(method)) {
    // delete(req, resp);
    // }
    // //鍒�
    // else if ("update".equals(method)) {
    // update(req, resp);
    // }
    // //鏀�
    // else if ("list".equals(method)) {
    // list(req, resp);
    // }
    // //鏌�
    }



    //娣诲姞

    private void insert(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException
    {
    //棣栧厛璇诲彇鍓嶇�缃戦〉鏁版嵁骞跺皢鏁版嵁瀛樺叆鎴戝垱寤虹殑grade瀛樺偍绌洪棿閲岋紝鍗� jsp/html 鈫� servlet

    req.setCharacterEncoding("utf-8"); //浠庣綉椤佃幏寰楁暟鎹�
    String username=req.getParameter("username");
    String password=req.getParameter("password");
    //鍥犱负req.getParameter鍙�兘鑾峰緱String绫诲瀷鐨勬暟鎹�
    //鎵�浠ヤ笅闈㈤渶瑕佺敤Integer.parseInt锛坮eq.getParameter()锛夋潵鎶奡tring绫诲瀷鐨勬暟鎹�浆鎹㈡垚鎴戜滑鎵�闇�鐨刬nt鏁版嵁
    String sex=req.getParameter("sex");
    String name=req.getParameter("name");
    String number=req.getParameter("number");
    String email=req.getParameter("email");
    String clas=req.getParameter("clas");
    String xi=req.getParameter("xi");
    String banji=req.getParameter("banji");
    String address=req.getParameter("address");
    String iphone=req.getParameter("iphone");
    String beizhu=req.getParameter("beizhu");
    User user=new User(username,password,sex,name,number,email,clas,xi,banji,address,iphone,beizhu);


    //鍏舵�杩涜�鎶奼rade閲岀殑鏁版嵁涓庢坊鍔犲埌鏁版嵁搴撻噷锛屽嵆 servlet 鈫� mysql鏁版嵁搴�
    //骞惰烦杞�紝req.getRequestDispatcher("add.jsp").forward(req, resp);杩欎竴姝ユ槸璺宠浆鍒板墠绔痡sp/html鐣岄潰

    if(service.insert(user)) //濡傛灉娣诲姞鎴愬姛
    {
    req.setAttribute("message", "娣诲姞鎴愬姛");
    //杩欓噷琛ㄧず锛屽湪req鐨勪綔鐢ㄥ煙涓��缃�竴涓猰assage鍙橀噺,鍙橀噺鍐呭�鏄�細娣诲姞鎴愬姛
    //鐒跺悗锛屽綋璺宠浆鍒板畠涓嬩竴涓�晫闈㈡椂锛岃繖涓�綔鐢ㄥ煙閲岀殑鍊煎彲浠ラ�氳繃Object message =request.getAttribute("message")鎷垮嚭鏉ヤ娇鐢�
    req.getRequestDispatcher("index.jsp").forward(req, resp);
    //澶勭悊瀹屼簡锛屽垎鍙戝埌涓嬩竴涓狫SP椤甸潰鎴栬�呬笅涓�涓狝ction缁х画澶勭悊銆�
    }
    else
    {
    req.setAttribute("message", "娣诲姞閲嶅�,璇烽噸鏂拌緭鍏�");
    req.getRequestDispatcher("fail.jsp").forward(req, resp);
    }
    }


    // //鍒犻櫎delete
    //
    // private void delete(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
    // {
    // req.setCharacterEncoding("UTF-8");
    // int id=Integer.parseInt(req.getParameter("id"));
    // service.delete(id); //杩涜�鏁版嵁搴撶殑鍒犻櫎鎿嶄綔
    // req.setAttribute("message", "鍒犻櫎鎴愬姛");
    // req.getRequestDispatcher("servlet?method=list").forward(req, resp);
    // }
    //
    // //淇�敼update
    //
    // private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
    // req.setCharacterEncoding("utf-8");
    // String id=req.getParameter("id");
    // int Chinese=Integer.parseInt(req.getParameter("Chinese"));
    // int Math=Integer.parseInt(req.getParameter("Math"));
    // int English=Integer.parseInt(req.getParameter("English"));
    // int SUM=Integer.parseInt(req.getParameter("SUM"));
    // Grade grade=new Grade(id,Chinese,Math,English,SUM);
    // service.update(grade);//杩涜�鏁版嵁搴撶殑淇�敼鎿嶄綔
    // req.setAttribute("message", "淇�敼鎴愬姛");
    // req.getRequestDispatcher("servlet?method=list").forward(req,resp);
    // //?method=list琛ㄧず浼犱竴涓�彨鍋歮ethod鐨勫弬鏁帮紝浠栫殑鍊兼槸list,浣犲彲浠ュ湪Servlet涓�敤request.getParam...鑾峰彇鍒般��
    // }
    //
    //
    // //鍏ㄩ儴
    //
    // private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
    // req.setCharacterEncoding("utf-8");
    // List<Grade> grade_list = service.list();
    // req.setAttribute("grade_list", grade_list);
    // req.getRequestDispatcher("list.jsp").forward(req,resp);
    // }
    //

    }

    User.java

    package test;

    public class User
    {
    private String username;
    private String password;
    private String sex;
    private String name;
    private String number;
    private String email;
    private String clas;
    private String xi;
    private String banji;
    private String address;
    private String iphone;
    private String beizhu;
    public User(String username, String password,String sex,String name,String number,String email,String clas,String xi,String banji,String address,String iphone,String beizhu) {
    this.username = username;
    this.password = password;
    this.sex = sex;
    this.name = name;
    this.number = number;
    this.email = email;
    this.clas = clas;
    this.xi = xi;
    this.banji = banji;
    this.address = address;
    this.iphone = iphone;
    this.beizhu=beizhu;
    }
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getSex() {
    return sex;
    }
    public void setSex(String sex) {
    this.sex = sex;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getNumber() {
    return number;
    }
    public void setNumber(String number) {
    this.number = number;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public String getClas() {
    return clas;
    }
    public void setClas(String clas) {
    this.clas = clas;
    }
    public String getXi() {
    return xi;
    }
    public void setXi(String xi) {
    this.xi = xi;
    }
    public String getBanji() {
    return banji;
    }
    public void setBanji(String banji) {
    this.banji = banji;
    }
    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    public String getIphone() {
    return iphone;
    }
    public void setIphone(String iphone) {
    this.iphone = iphone;
    }
    public String getBeizhu() {
    return beizhu;
    }
    public void setBeizhu(String beizhu) {
    this.beizhu = beizhu;
    }
    }

    fail.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <h>添加失败</h>

    </body>
    </html>

    index.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <center>
    <h1>
    <a href="register.jsp">主页</a>
    </h1>
    </center>
    </body>
    </html>

    register.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Insert title here</title>
    <meta charset="UTF-8">
    <title>注册页面</title>
    </head>
    <body>
    <%
    Object message = request.getAttribute("message");
    if(message!=null && !"".equals(message)){

    %>
    <script type="text/javascript">
    alert("<%=request.getAttribute("message")%>");
    </script>
    <%} %>
    <div align="center">
    <center> <h6 style="color: black;">注册</h6></center>

    <form name = "form1" action="servlet?method=insert" method="post" onsubmit="return check_submit()">
    <table>
    <tr>
    <td>登陆账号</td>
    <td colspan="2"> <input type="text" id="username" name="username" maxlength="12" onblur="blur_id()" onfocus="focus_id()"/></td>
    <td width="300"><div id="result"></td>
    </tr>
    <tr>
    <td>登录密码</td>
    <td colspan="2"> <input type="password" id="password" name="password" onblur="blur_pass()" onfocus="focus_pass()" /></td>
    <td width="300"><div id="result1"></td>
    </tr>
    <tr>
    <td>性别:</td>
    <td colspan="2"><select name="sex"id="sex" >
    <option value="男" >男</option>
    <option value="女" >女</option>
    </select></td>
    </tr>
    <tr>
    <td>姓名</td>
    <td colspan="2"><input type="text" id="name" name="name" /></td>
    </tr>
    <tr>
    <td>学号</td>
    <td colspan="2"><input type="text" id="number" name="number" /></td>
    </tr>
    <tr>
    <td>邮箱</td>
    <td colspan="2"><input type="email" id="email" name="email" ></td>
    </tr>


    <tr>
    <td>所在学院:</td>
    <td colspan="2"><input type="text" id="clas" name="clas" >
    </td>
    </tr>
    <tr>
    <td>所在系:</td>
    <td colspan="2"><input type="text" id="xi" name="xi" >
    </td>
    </tr>
    <tr>
    <td>所在班级</td>
    <td colspan="2"><input type="text" id="banji" name="banji"></td>
    </tr>
    <tr>
    <td>入学年份</td>
    <td colspan="2"><select name="address"id="address" >
    <option value="1998" >1998</option>
    <option value="1999" >1999</option>
    </select>届</td>
    </tr>
    <tr>
    <td>生源地</td>
    <td colspan="2"><input type="text" id="iphone" name="iphone"></td>
    </tr>
    <tr>
    <td>备注</td>
    <td colspan="2"><input type="text" id="beizhu" name="beizhu"></td>
    </tr>
    <tr>
    <td colspan="3"> <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="reset" class="b">重&nbsp;&nbsp;&nbsp;置</button></td>

    </tr>
    </table>
    </form>
    </div>
    <script type="text/javascript">
    /*
    表单验证
    */
    var flag = false; // flag 如果为true(即用户名合法)就允许表单提交, 如果为false(即用户名不合法)阻止提交
    function focus_pass()
    {
    var nameObj = document.getElementById("result1");
    nameObj.innerHTML = "由六位字符和数字组成";
    nameObj.style.color="#999";
    }
    function blur_pass()
    {
    var nameObj = document.getElementById("result1");
    // 判断用户名是否合法
    var str2 = check_user_pass(document.form1.password.value);
    nameObj.style.color="red";
    if ("密码合法" == str2)
    {
    flag = true;
    nameObj.innerHTML = str2;
    }
    else
    {
    nameObj.innerHTML = str2;
    }
    }

    function check_user_pass(str)
    { var str2 = "密码合法";
    if ("" == str)
    {
    str2 = "密码为空";
    return str2;
    }
    else if (str.length<8)
    {
    str2 = "用户名应是六位以上组成";
    return str2;
    }
    else if (!check_word(str))
    {
    str2 = "未含有英文字符";
    return str2;
    }

    return str2;


    }

    function focus_id()
    {
    var nameObj = document.getElementById("result");
    nameObj.innerHTML = "由六到十二英文字符和数字组成";
    nameObj.style.color="#999";
    }
    function blur_id()
    {
    var nameObj = document.getElementById("result");
    // 判断用户名是否合法
    var str2 = check_user_id(document.form1.username.value);
    nameObj.style.color="red";
    if ("用户名合法" == str2)
    {
    flag = true;
    nameObj.innerHTML = str2;
    }
    else
    {
    nameObj.innerHTML = str2;
    }
    }

    function check_user_id(str)
    {
    var str2 = "用户名合法";
    if ("" == str)
    {
    str2 = "用户名号为空";
    return str2;
    }
    else if ((str.length<=5)||(str.length>=13))
    {
    str2 = "用户名应是六到十二位组成";
    return str2;
    }
    else if (!check_word(str))
    {
    str2 = "未含有英文字符";
    return str2;
    }
    else if(!check_firstword(str))
    {
    str2 = "必须以英文字母开头";
    return str2;
    }
    return str2;
    }

    // 当鼠标聚焦于用户名
    function focus_phone()
    {
    // 找到后面的div, id = result_name
    var nameObj = document.getElementById("result_name");
    nameObj.innerHTML = "手机号不能包含特殊字符且为11位";
    nameObj.style.color="#999";
    }
    // 当鼠标不聚焦于用户名input
    function blur_phone()
    {
    // 找到id=result_name的div
    var nameObj = document.getElementById("result_name");
    // 判断用户名是否合法
    var str2 = check_user_name(document.form1.no.value);
    nameObj.style.color="red";
    if ("手机号合法" == str2)
    {
    flag = true;
    nameObj.innerHTML = str2;
    }
    else
    {
    nameObj.innerHTML = str2;
    }

    }
    // 检查用户名是否合法 合法就返回"该用户名合法"
    function check_user_name(str)
    {
    var str2 = "手机号合法";
    if ("" == str)
    {
    str2 = "手机号为空";
    return str2;
    }
    else if (str.length!=11)
    {
    str2 = "手机号必须为11位";
    return str2;
    }
    else if (check_other_char(str))
    {
    str2 = "不能含有特殊字符";
    return str2;
    }
    return str2;
    }
    function check_firstword(str)
    { var arr = ["a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
    for (var i = 0; i < arr.length; i++)
    {
    if (arr[i] == str.charAt(0))
    {
    return true;
    }
    }
    return false;
    }


    function check_word(str)
    { var arr = ["_","a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
    for (var i = 0; i < arr.length; i++)
    {
    for (var j = 0; j < str.length; j++)
    {
    if (arr[i] == str.charAt(j))
    {
    return true;
    }
    }
    }
    return false;
    }

    // 验证用户名是否含有特殊字符
    function check_other_char(str)
    {
    var arr = ["&", "\", "/", "*", ">", "<", "@", "!"];
    for (var i = 0; i < arr.length; i++)
    {
    for (var j = 0; j < str.length; j++)
    {
    if (arr[i] == str.charAt(j))
    {
    return true;
    }
    }
    }
    return false;
    }
    // 根据验证结果确认是否提交
    function check_submit()
    {
    if (flag == false)
    {
    return false;
    }
    return true;
    }
    </script>
    </head>
    </body>
    </html>

    二、运行结果:

    三、数据库储存:

  • 相关阅读:
    练习系列 5、求子数组的最大和
    练习系列 8、m进制转n进制(任意进制转换)
    bind1st/bind2nd与mem_fun组合使用的问题
    练习系列 4、异质链表
    幻方常规解法汇总
    练习系列 7、打印数据的二进制表示
    C++成员函数指针错误用法警示
    FreeBSD启动出现My unqualified host name unkown...Sleeping for retry...解决方案
    从DLL导出.a文件
    WIN7桌面无反应解决方案
  • 原文地址:https://www.cnblogs.com/xiangyu721/p/11716631.html
Copyright © 2020-2023  润新知