在插入数据库之前验证表单控件

时间:2015-08-03 08:15:57

标签: javascript mysql spring-mvc tomcat

我想将记录插入数据库(MySQL)但在插入之前我想验证表单字段是否由用户填写,它必须验证提交按钮onclick并且我插入记录使用与java文件相关的注释方法,因此,当我尝试使用spring注释方法插入记录并验证JavaScript中的记录时,它给出了以下错误:

  

HTTP状态500 - 请求处理失败;嵌套异常是java.lang.NumberFormatException:对于输入字符串:“”

我的javaScript:

<script type="text/javascript">
    function validateFields()
    {
        var c_name = document.formregisterclinic.ctl00$cphMaster$txtClinicName.value;
        var p_no = document.formregisterclinic.ctl00$cphMaster$txtPhone.value;
        var st_address = document.formregisterclinic.ctl00$cphMaster$txtStreetAddress.value;
        var state = document.formregisterclinic.ctl00$cphMaster$txtState.value;
        var city = document.formregisterclinic.ctl00$cphMaster$txtCity.value;}
        var zip_code = document.formregisterclinic.ctl00$cphMaster$txtZipCode1.value;
        var f_name = document.formregisterclinic.ctl00$cphMaster$txtCPFName.value;
        var l_name = document.formregisterclinic.ctl00$cphMaster$txtCPLName.value;
        var email = document.formregisterclinic.ctl00$cphMaster$txtEmail.value;
        var cell_no = document.formregisterclinic.ctl00$cphMaster$txtCellPhone.value;
        var u_name = document.formregisterclinic.ctl00$cphMaster$txtUserName.value;
        var pass = document.formregisterclinic.ctl00$cphMaster$txtPassword.value;
        var c_pass = document.formregisterclinic.ctl00$cphMaster$txtConfirmPassword.value;

        if(c_name == "" || c_name == null)
        {
            alert("Clinic name can't be blank");
            return false;
        }
        else if(p_no == "" || p_no == null)
        {
            alert("Phone number can't be blank");
            return false;
        }
        else if(st_address == "" || st_address == null)
        {
            alert("Street address can't be blank");
            return false;
        }
        else if(state == "" || state == null)
        {
            alert("State can't be blank");
            return false;
        }
        else if(city == "" || city == null)
        {
            alert("City can't be blank");
            return false;
        }
        else if(zip_code == "" || zip_code == null)
        {
            alert("Zip code can't be blank");
            return false;
        }
        else if(f_name == "" || f_name == null)
        {
            alert("First name can't be blank");
            return false;
        }
        else if(l_name == "" || l_name == null)
        {
            alert("Last name can't be blank");
            return false;
        }
        else if(email == "" || email == null)
        {
            alert("Email can't be blank");
            return false;
        }
        else if(cell_no == "" || cell_no == null)
        {
            alert("Cell number can't be blank");
            return false;
        }
        else if(u_name == "" || u_name == null)
        {
            alert("User name can't be blank");
            return false;
        }
        else if(pass == "" || pass == null)
        {
            alert("Password can't be blank");
            return false;
        }
        else if(c_pass == "" || c_pass == null)
        {
            alert("Confirm password can't be blank");
            return false;
        }
</script>

我的映射方法:

@RequestMapping(value = "/registerclinic", method = RequestMethod.POST)
public String registerclinicdbconnection(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res)throws ServletException 
{
    String clinic_name = req.getParameter("ctl00$cphMaster$txtClinicName");
    long phone_no = Long.parseLong(req.getParameter("ctl00$cphMaster$txtPhone"));
    String street_add = req.getParameter("ctl00$cphMaster$txtStreetAddress");
    String state = req.getParameter("ctl00$cphMaster$txtState");
    String city = req.getParameter("ctl00$cphMaster$txtCity");
    int zip_code = Integer.valueOf(req.getParameter("ctl00$cphMaster$txtZipCode1"));
    String first_name =  req.getParameter("ctl00$cphMaster$txtCPFName");
    String last_name =  req.getParameter("ctl00$cphMaster$txtCPLName");
    String email = req.getParameter("ctl00$cphMaster$txtEmail");
    long cell_phone = Long.parseLong(req.getParameter("ctl00$cphMaster$txtCellPhone"));
    String user_name = req.getParameter("ctl00$cphMaster$txtUserName");
    String password = req.getParameter("ctl00$cphMaster$txtPassword");
    String c_password = req.getParameter("ctl00$cphMaster$txtConfirmPassword");


    // Inserting records to register the clinic by making connection with database.
    String queryText = "insert into registerclinic(clinicname,phone,streetadd,state,city,zipcode,firstname,lastname,email,cellphone,username,password) values('"+clinic_name+"','"+phone_no+"','"+street_add+"','"+state+"','"+city+"','"+zip_code+"','"+first_name+"','"+last_name+"','"+email+"','"+cell_phone+"','"+user_name+"','"+password+"')";
    try
    {
        Connection con = null;
        Class.forName("com.mysql.jdbc.Driver");
        con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/clinicmanagement","root","dipak");
        Statement stat = con.createStatement();
        stat.executeUpdate(queryText);
        System.out.println("Record has been inserted");
        stat.close();
        con.close();
        return "success";
    }catch(Exception ea)
    { 
        System.out.println("Exception Occured.."+ ea);
        return "fail";
    }
}

1 个答案:

答案 0 :(得分:0)

好像你的validateFields()方法

结束时没有返回true

尝试以下:

function validateFields()
    {
        var c_name = document.formregisterclinic.ctl00$cphMaster$txtClinicName.value;
        var p_no = document.formregisterclinic.ctl00$cphMaster$txtPhone.value;
        var st_address = document.formregisterclinic.ctl00$cphMaster$txtStreetAddress.value;
        var state = document.formregisterclinic.ctl00$cphMaster$txtState.value;
        var city = document.formregisterclinic.ctl00$cphMaster$txtCity.value;}
        var zip_code = document.formregisterclinic.ctl00$cphMaster$txtZipCode1.value;
        var f_name = document.formregisterclinic.ctl00$cphMaster$txtCPFName.value;
        var l_name = document.formregisterclinic.ctl00$cphMaster$txtCPLName.value;
        var email = document.formregisterclinic.ctl00$cphMaster$txtEmail.value;
        var cell_no = document.formregisterclinic.ctl00$cphMaster$txtCellPhone.value;
        var u_name = document.formregisterclinic.ctl00$cphMaster$txtUserName.value;
        var pass = document.formregisterclinic.ctl00$cphMaster$txtPassword.value;
        var c_pass = document.formregisterclinic.ctl00$cphMaster$txtConfirmPassword.value;

        if(c_name == "" || c_name == null)
        {
            alert("Clinic name can't be blank");
            return false;
        }
        else if(p_no == "" || p_no == null)
        {
            alert("Phone number can't be blank");
            return false;
        }
        else if(st_address == "" || st_address == null)
        {
            alert("Street address can't be blank");
            return false;
        }
        else if(state == "" || state == null)
        {
            alert("State can't be blank");
            return false;
        }
        else if(city == "" || city == null)
        {
            alert("City can't be blank");
            return false;
        }
        else if(zip_code == "" || zip_code == null)
        {
            alert("Zip code can't be blank");
            return false;
        }
        else if(f_name == "" || f_name == null)
        {
            alert("First name can't be blank");
            return false;
        }
        else if(l_name == "" || l_name == null)
        {
            alert("Last name can't be blank");
            return false;
        }
        else if(email == "" || email == null)
        {
            alert("Email can't be blank");
            return false;
        }
        else if(cell_no == "" || cell_no == null)
        {
            alert("Cell number can't be blank");
            return false;
        }
        else if(u_name == "" || u_name == null)
        {
            alert("User name can't be blank");
            return false;
        }
        else if(pass == "" || pass == null)
        {
            alert("Password can't be blank");
            return false;
        }
        else if(c_pass == "" || c_pass == null)
        {
            alert("Confirm password can't be blank");
            return false;
        }


        return true;
    }