javascript - Validating form controls before inserting to database -
i want insert records database(mysql) before insertion want validate form fields whether filled users or not, must validate onclick
of submit button , inserting records using annotation method related java file so, when trying insert records using spring annotation method , validating records in javascript gives me following error:
http status 500 - request processing failed; nested exception java.lang.numberformatexception: 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 blank"); return false; } else if(p_no == "" || p_no == null) { alert("phone number can't blank"); return false; } else if(st_address == "" || st_address == null) { alert("street address can't blank"); return false; } else if(state == "" || state == null) { alert("state can't blank"); return false; } else if(city == "" || city == null) { alert("city can't blank"); return false; } else if(zip_code == "" || zip_code == null) { alert("zip code can't blank"); return false; } else if(f_name == "" || f_name == null) { alert("first name can't blank"); return false; } else if(l_name == "" || l_name == null) { alert("last name can't blank"); return false; } else if(email == "" || email == null) { alert("email can't blank"); return false; } else if(cell_no == "" || cell_no == null) { alert("cell number can't blank"); return false; } else if(u_name == "" || u_name == null) { alert("user name can't blank"); return false; } else if(pass == "" || pass == null) { alert("password can't blank"); return false; } else if(c_pass == "" || c_pass == null) { alert("confirm password can't 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 register clinic making connection database. string querytext = "insert 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"; } }
seems though don't have return true @ end of validatefields()
method
try below:
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 blank"); return false; } else if(p_no == "" || p_no == null) { alert("phone number can't blank"); return false; } else if(st_address == "" || st_address == null) { alert("street address can't blank"); return false; } else if(state == "" || state == null) { alert("state can't blank"); return false; } else if(city == "" || city == null) { alert("city can't blank"); return false; } else if(zip_code == "" || zip_code == null) { alert("zip code can't blank"); return false; } else if(f_name == "" || f_name == null) { alert("first name can't blank"); return false; } else if(l_name == "" || l_name == null) { alert("last name can't blank"); return false; } else if(email == "" || email == null) { alert("email can't blank"); return false; } else if(cell_no == "" || cell_no == null) { alert("cell number can't blank"); return false; } else if(u_name == "" || u_name == null) { alert("user name can't blank"); return false; } else if(pass == "" || pass == null) { alert("password can't blank"); return false; } else if(c_pass == "" || c_pass == null) { alert("confirm password can't blank"); return false; } return true; }
Comments
Post a Comment