visio中修剪_修剪Javascript中的字符串

visio中修剪

User input plays a big role on webpages. It acts as an interface between users and the server. Handling user inputs is important as we need to ensure what data can be sent to the server and we cannot expect what the user will send. One of the most common things need to be checked is the input strings.

用户输入在网页上扮演着重要角色。 它充当用户和服务器之间的接口。 处理用户输入很重要,因为我们需要确保可以将什么数据发送到服务器,而我们不能期望用户将发送什么。 需要检查的最常见的事情之一是输入字符串。

Strings in javascript is not hard but also not easy to be managed. String is an object in javascript and so a lot of methods have been designed to manipulate it but on the other hand, as an input type, it provides the most freedom to users. As a result, checking is also the most complicated with text input type.

javascript中的字符串并不难,但也不容易管理。 字符串是javascript中的对象,因此已经设计了许多方法来操纵它,但是另一方面,作为输入类型,它为用户提供了最大的自由度。 结果,对于文本输入类型,检查也是最复杂的。

This time, we take the start and trailing space in text input as an example. Users, mostly casual ones, sometimes input strings with these whitespaces in the input boxes without caring. It does'nt look much difference in a glance. However, in the server side point of view, these start and trailing space does make a lot difference. For example, for a query request and database update request, these spaces can lead to failure of potential hits in the search and failure in finding the existing database entry. So, removing these starting and trailing space is a very common practice. It can be done on the client side when user submit the data.

这次,我们以文本输入中的开始和结尾空格为例。 用户(通常是休闲用户)有时会在输入框中输入带有这些空格的字符串,而无需关心。 一眼看上去并没有太大的区别。 但是,从服务器的角度来看,这些起始和结尾空间确实有很大的不同。 例如,对于查询请求和数据库更新请求,这些空间可能导致搜索中的潜在命中失败以及找不到现有数据库条目的失败。 因此,删除这些起始和尾随空间是非常普遍的做法。 当用户提交数据时,可以在客户端完成。

Sounds good! Wait! Javascript object does'nt provide the trim function! So, how? Actually, it's not hard to make one trim function by yourself. The following is one of the examples using javascript regular expressions.

听起来不错! 等待! Javascript对象不提供trim功能! 又怎样? 实际上,自己完成一项修剪功能并不难。 以下是使用javascript正则表达式的示例之一。

function trim(str)
{
	var startpatt = /^\s/;
	var endpatt = /\s$/;
	
	while(str.search(startpatt) == 0)
		str = str.substring(1, str.length);
	
	while(str.search(endpatt) == str.length-1)
		str = str.substring(0, str.length-1);	
	
	return str;
}

The function trim() accept a string object and remove any starting and trailing whitespaces (spaces,tabs and newlines) and return the trimmed string. You can use this function to trim form inputs to ensure valid data to be sent.

函数trim()接受一个字符串对象,并删除所有开头和结尾的空格(空格,制表符和换行符),并返回修剪后的字符串。 您可以使用此功能修剪表单输入,以确保发送有效数据。

The following function uses the trim() function and trim all text input of a form

以下函数使用trim()函数并修剪表单的所有文本输入

function submittrim(form)
{
	for (var i = 0; i<form.elements.length; i++) 
	{
		if(form.elements[i].value != '' && form.elements[i].type == 'text' )
		{
			form.elements[i].value = trim(form.elements[i].value);
		}
	}
	
	return true;
}
</eeSnippet>

And you can trigger the call with the onSubmit event in your form declaration.

[code]
<form method="POST" action="test.do" onsubmit="return submittrim(this);">

翻译自: https://www.experts-exchange.com/articles/191/Trimming-strings-in-Javascript.html

visio中修剪

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值