Verifying input fields prior to database entry

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";
    }
}

Answer №1

It appears that the validateFields() method is missing a return true statement at the end.

You can update it as follows:

function validateFields() {
    var clinicName = document.formregisterclinic.ctl00$cphMaster$txtClinicName.value;
    var phoneNumber = document.formregisterclinic.ctl00$cphMaster$txtPhone.value;
    var streetAddress = document.formregisterclinic.ctl00$cphMaster$txtStreetAddress.value;
    var state = document.formregisterclinic.ctl00$cphMaster$txtState.value;
    var city = document.formregisterclinic.ctl00$cphMaster$txtCity.value;
    var zipCode = document.formregisterclinic.ctl00$cphMaster$txtZipCode1.value;
    var firstName = document.formregisterclinic.ctl00$cphMaster$txtCPFName.value;
    var lastName = document.formregisterclinic.ctl00$cphMaster$txtCPLName.value;
    var email = document.formregisterclinic.ctl00$cphMaster$txtEmail.value;
    var cellPhone = document.formregisterclinic.ctl00$cphMaster$txtCellPhone.value;
    var userName = document.formregisterclinic.ctl00$cphMaster$txtUserName.value;
    var password = document.formregisterclinic.ctl00$cphMaster$txtPassword.value;
    var confirmPassword = document.formregisterclinic.ctl00$cphMaster$txtConfirmPassword.value;

    if (clinicName === "" || clinicName === null) {
        alert("Clinic name cannot be blank");
        return false;
    } else if (phoneNumber === "" || phoneNumber === null) {
        alert("Phone number cannot be blank");
        return false;
    } else if (streetAddress === "" || streetAddress === null) {
        alert("Street address cannot be blank");
        return false;
    } else if (state === "" || state === null) {
        alert("State cannot be blank");
        return false;
    } else if (city === "" || city === null) {
        alert("City cannot be blank");
        return false;
    } else if (zipCode === "" || zipCode === null) {
        alert("Zip code cannot be blank");
        return false;
    } else if (firstName === "" || firstName === null) {
        alert("First name cannot be blank");
        return false;
    } else if (lastName === "" || lastName === null) {
        alert("Last name cannot be blank");
        return false;
    } else if (email === "" || email === null) {
        alert("Email cannot be blank");
        return false;
    } else if (cellPhone === "" || cellPhone === null) {
        alert("Cell phone cannot be blank");
        return false;
    } else if (userName === "" || userName === null) {
        alert("User name cannot be blank");
        return false;
    } else if (password === "" || password === null) {
        alert("Password cannot be blank");
        return false;
    } else if (confirmPassword === "" || confirmPassword === null) {
        alert("Confirm password cannot be blank");
        return false;
    }

    return true;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Query to determine the total occurrences of numbers in all preceding rows

My table view consists of the following data: Id, Picks 5, 1 5, 5 5, 10 5, 20 4, 8 4, 10 4, 11 4, 22 3, 1 3, 8 3, 10 3, 25 2, 3 2, 5 2, 23 2, 24 1, 14 1, 17 1, 20 1, 24 The table has two columns: Id and Picks. Each id is repeated four times for a dra ...

Listening for key combinations in VueJS using a mounted event listener

I am facing an issue with my global key listener - it only catches single key strokes. How can I modify it to capture combinations like ctrl+enter? mounted() { window.addEventListener ( "keypress", e => { console.log ...

Redirecting in PHP based on this error message

(Please bear with me.) I am looking to automatically redirect users to a specific page in case an error occurs, specifically if there is a duplicate entry in the database. This situation usually arises when something goes wrong during submission to the dat ...

Having trouble resolving bugs in a PHP script designed to update the status of 'open' or 'close' in a database

Seeking some assistance! :) Context of the issue: I manage an SQL database containing unique items with three columns: [index] [id] [status] [index] = incremented number [id] = unique string identifier [status] = '0' for open worksta ...

Running npm commands, such as create-react-app, without an internet connection can be a

Currently, I am working in an offline environment without access to the internet. My system has node JS installed. However, whenever I attempt to execute the npm create-react-app command, I encounter an error. Is there a workaround that would allow me to ...

Which programming language is ideal for powering the backend of a 'social' platform?

When it comes to my website, my top priorities are connections and graph matching. I'm considering using MySql to handle friend connections, similar to Twitter, but I'm unsure if it's the best choice for implementing graph algorithms like fi ...

The jQuery call stack size is exceeded when attempting to move focus to the next input field

Greetings, fellow users! I've encountered an issue with a form on my website. Here's the code snippet: <form name="data" action="#" method="post"> <input type="text" name="data" maxlength="1" class="a"> <input type="text" name="d ...

Use the date-fns max function to identify the latest date in an array of date strings

The Dates string array is created using data from the backend object in the following manner: const dates: string[] = this.data.map((item:any) => item.date.jsdate); Here is the resulting array: dates = [ Thu Jan 12 2015 00:00:00 GMT+0530 (India T ...

Utilizing multiple API calls to initiate JSON data and seamlessly integrate it across the entire system

I am currently developing a project that utilizes Vue.js and Laravel for implementation. The project is focused on academia and consists of units, lessons, and activities, all interrelated. Each unit contains multiple lessons, and each lesson contains mult ...

Tips for overriding a CSS class without impacting other classes sharing the same name

Within a page filled with various widgets, I am facing an issue where I need to override the container class for a specific widget. However, the problem is that all widgets with the same class name are being impacted. Attached below is a screenshot from in ...

Unpacking and reassigning variables in Vue.js 3 using TypeScript

I am working with a component that has input parameters, and I am experimenting with using destructuring assignment on the properties object to reassign variables with different names: <script setup lang="ts"> const { modelValue: isSelected ...

What is the best way to eliminate mouse click release events?

Encountering an issue with my Vue/Vuetify Dialog that closes when clicking outside of it as expected. The problem arises when there is a text field inside the dialog. If I accidentally select the content and release the mouse outside of the dialog, it als ...

An easy CSS conundrum when hovering

Looking for some assistance with CSS. I want to display the text "Featured Page" with a picture appearing on the right side upon hovering (mouseover). Currently, the picture shows up under the text using the following CSS, but I need it to be larger and pl ...

Select the text inside the current cell of a jqGrid table

The issue at hand is that when using jqGrid with cellEdit:true, users are unable to select text in a cell. Once the mouse button is released, the selection resets. This limitation prevents users from copying the selected text within cells. Has anyone enco ...

What is the most effective way to create a straightforward user interface in Node Js for the purpose of automation?

Currently, I am employing Selenium webdriver alongside Javascript and Node JS for automating test cases. My initial test case looks like this : var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder(). withCapabili ...

Tips on incorporating variable tension feature into D3 hierarchical edge bundling

I found a d3 sample on hierarchical edge bundling that I am experimenting with - My main focus is how to add tension functionality to the example provided at the following link (code can be found here): While I have reviewed the code in the first link, I ...

The orderBy function is not functioning properly when used in conjunction with ng

I attempted to replicate the functionality demonstrated here, which involves organizing an array of objects in the view using the filter orderBy. While my code is functioning properly, when I apply the orderBy filter, nothing appears in the view. You can ...

In what way can a random choice capture the correct correspondent number?

$("#question2").hide(); const options = [$(".option1"), $(".option2"), $(".option3"), $(".option4")]; let randomOption = function() { const texts = options .map(opt => opt[0].textContent) .sort(() => .5 - Math.random()); $(".option1").t ...

"JIRA post functionality not functioning correctly on Internet Explorer and Firefox due to jQuery compatibility

I am facing an issue while trying to submit data to JIRA. It seems to be working fine in Chrome but not in IE/FF. I suspect there is something wrong with the ajax call setup, but I'm not entirely sure what needs to be done to fix it. $(&apos ...

What is the solution for the error "Build error occurred ReferenceError: self is not defined" when building a NextJs application?

pages/components/moru-sdk.js // https://libraries.io/npm/moru-web-sdk import { MoruCheckout } from "moru-web-sdk"; function MoruService() { const options = { access_key: "test_9425294388834bdface7d1b58fd538bf67627d9408fe4f258982 ...