使用trim()删除空格,但仍未得到预期的输出 [英] Using trim() to remove spaces,but still didn't get expected output

查看:196
本文介绍了使用trim()删除空格,但仍未得到预期的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在开发基于Spring MVC的Web应用程序,应用程序显示数据列表,我也便于过滤器选项以增强搜索功能,我还通过使用trim()删除额外空间,但现在发生了什么,当用户输入时文本字段中的数据并输入相应的结果将显示在列表中,但如果在输入后添加空格,结果将是NOT FOUND,即使我也在javascript中处理空格



从数据库中获取数据的Java代码

  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代码:代表id获取值

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

我还附加了两个带空格的屏幕截图,没有空格



没有空格
随着空间

解决方案

我建议在服务器端修剪。

在服务器端验证总是更好,因为我们可以为不同的UI应用程序使用相同的服务代码。请求可能包含错误或篡改的数据。

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


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

Without space With space

解决方案

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

这篇关于使用trim()删除空格,但仍未得到预期的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆