input输入框自动消除空格

今天遇到了一个问题,用户在在前端的input里面输入id的时候,多写了个空格,数据库里面就找不到id了,所以无法获取输入的id所绑定的标签位置在哪里,现在需要在前端做一个处理,input框输入的数值里面,会自动消除空格。

代码示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
	<body>
		<div class="modal-body form">			
				<div class="form-group">
					<label class="col-sm-2 control-label" >资产编号<span class="required"> * </span></label>
					<div class="col-sm-6">
						<input type="text" class="form-control" id="assetId" onkeyup="this.value=this.value.replace(/[, ]/g,'')"></input>						
					</div>
				</div>		
		</div>
	</body>
</html>

这段代码的作用:

onkeyup="this.value=this.value.replace(/[, ]/g,'')"

1:在input框里面输入编号的过程中,出现空格,自动消除
2:input首尾出现空格,自动消除
3:复制粘贴的文本里面出现空格,自动消除


原文作者:祈澈姑娘 关注「编程微刊」公众号 ,在微信后台回复「小程序」,获取小程序开发全套资料和500G编程资源教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值