In order to insert records into the MySQL database, I need to validate the form fields before insertion to ensure they are filled out by users. The validation should occur on the onClick event of the submit button. I am using an annotation method related to a Java file for inserting the records. However, when attempting to insert the records using the Spring annotation method and validating them in JavaScript, I encounter the following error:
HTTP Status 500 - Request processing failed; nested exception is java.lang.NumberFormatException: For input string: ""
My 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>
My mapping method:
@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";
}
}