Javascript 从表单输入中修剪空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8120983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trim spaces from form input?
提问by user1031152
Hello so i'm trying to trim the white spaces when the user enter spaces he get an error explaning that he should enter valid chars but if he used: ' test '
the same value is entered in the database do i need to trim that again in javascript ?
您好,所以我正在尝试在用户输入空格时修剪空格,他收到错误,说明他应该输入有效字符,但如果他使用:' test '
在数据库中输入了相同的值,我是否需要在 javascript 中再次修剪它?
function validateForm()
{
if(trim(document.insert.aname.value) ==="")
{
alert("Animal should have a name");
document.insert.aname.focus();
return false;
}
}
function trim(value) {
return value.replace(/^\s+|\s+$/g,"");
}
please can you help ? the input page is .JSP
请问你能帮忙吗?输入页面是.JSP
回答by Eric
You're not actually trimming the input. You want this:
您实际上并没有修剪输入。你要这个:
//store the input in a variable
var nameInput = document.insert.aname
function validateForm() {
//Get the trimmed name
var animalName = trim(nameInput.value)
if(animalName) {
//Update the form with the trimmed value, just before the form is sent
nameInput.value = animalName
}
else {
//Trimmed value is empty
alert("Animal should have a name");
nameInput.focus();
return false;
}
}
function trim(value) {
return value.replace(/^\s+|\s+$/g,"");
}
回答by PhilDin
First, I'm going to offer some unsolicited advice:) Even if your javascript code trims the input from the user, you cannot guarantee that this code will always run. For example, a user might disable javascript on their browser or just use a library (lots of them exist) to programmatically submit form data without using any browser.
首先,我将提供一些不请自来的建议:) 即使您的 javascript 代码修剪了用户的输入,您也不能保证此代码将始终运行。例如,用户可能会在他们的浏览器上禁用 javascript,或者只使用一个库(其中很多存在)以编程方式提交表单数据而不使用任何浏览器。
So, you should always trim the input on the server side (in your case, in the JSP). In production systems, you need to do a little more than simply trimming, you need to check maximum sizes and data types as well (so if your JSP requires a numeric input, you need to check for that)
因此,您应该始终在服务器端(在您的情况下,在 JSP 中)修剪输入。在生产系统中,您需要做的不仅仅是修剪,还需要检查最大大小和数据类型(因此,如果您的 JSP 需要数字输入,则需要检查)
What this means is that you essentially have to do data validation twice. The data validation that you do in the browser is there to help the user and it should show a user friendly message when something invalid is entered. The important concept is that the browser validation is not there to provide security as it doesn't provide any. The security comes from the server side validation. It's not so important for server side validation to provide user friendly feedback; it's nice if it does but it's not the top priority.
这意味着您基本上必须进行两次数据验证。您在浏览器中进行的数据验证是为了帮助用户,当输入无效内容时,它应该显示用户友好的消息。重要的概念是浏览器验证不是为了提供安全性,因为它不提供任何安全性。安全性来自服务器端验证。服务器端验证提供用户友好的反馈并不重要;如果这样做很好,但这不是首要任务。
Now, to your question. When you call something like
现在,回答你的问题。当你打电话给类似的东西
trim(document.insert.aname.value)
this doesn't change the value in the form. It just gives back a trimmed version of the form data. So when you submit the form, the space at the end of 'test' goes along with the form data.
这不会改变表单中的值。它只是返回表单数据的修剪版本。因此,当您提交表单时,'test' 末尾的空格与表单数据一起出现。
It's possible for javascript to manipulate what is in the form but this is not commonly done and is not advisable. So, to summarise:
javascript 可以操作表单中的内容,但这并不常见,也不可取。所以,总结一下:
- Your code does not remove the trailing space for the user submitted data
- You could change you code to do this but it wouldn't be a good idea as you will have to perform the trim on the server regardless
- Therefore, you do need to trim it on the server
- 您的代码不会删除用户提交数据的尾随空格
- 您可以更改代码来执行此操作,但这不是一个好主意,因为无论如何您都必须在服务器上执行修剪
- 因此,您确实需要在服务器上修剪它
Hope this helps. Phil
希望这可以帮助。菲尔
回答by Saurabh Santhosh
Hey you can use the jQuery.trim
function for this:
嘿,您可以jQuery.trim
为此使用该功能:
var str = " a ";
str = $.trim(str)