user3677160
user3677160

Reputation:

Using trim() to remove spaces,but still didn't get expected output

Ok,i am developing spring MVC based web application, application shows data is list, and i also facilitate filter options to enhanced search functionality, I also remove extra space by using trim(), but what happening now, when user input data in text field and enter the corresponding result will be displayed into the list, but if space added after input, the result will be "NOT FOUND" even i handle the space in javascript too

Java Code which fetches data from database

 if (searchParamDTO.getRegNO().trim() != null && !searchParamDTO.getRegNO().trim().equals("") && !searchParamDTO.getRegNO().trim().equals("null")) {
                    query += " AND UR.REG_UNIQUE_ID = :REG_UNIQUE_ID ";
                    param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO());
                }

JavaScript Code: fetches the value in behalf of id

function setSearchParameters() {
    regNo = $('#regNo').val().trim();}

i also attached two screenshot with spaces and without spaces enter image description here

enter image description here

Without space With space

Upvotes: 1

Views: 241

Answers (1)

Manasi
Manasi

Reputation: 765

I would suggest to trim on server side as well.
It is always better to validate on server side as we can use same serve code for different UI applications And request could be with wrong or tampered data.

 String regNo = searchParamDTO.getRegNO().trim();
 if (regNo != null && !"".equals(regNo) && !"null".equals(regNo)) {
                    query += " AND UR.REG_UNIQUE_ID = :REG_UNIQUE_ID ";
                    param.addValue("REG_UNIQUE_ID", regNo);
                }

Upvotes: 1

Related Questions