通过js禁止输入空格(试用场景:当用字符串拼接插入dom节点时,onkeyup这些方法都不好使可用这个)
<span style="font-family: Arial, Helvetica, sans-serif;"><input type="password" name="password" id="password" onkeydown="return banInputSapce(event);" onKeyup="return inputSapceTrim(event,this);" /></span>
/**
* 是否去除所有空格
* @param str
* @param is_global 如果为g或者G去除所有的
* @returns
*/
function Trim(str,is_global)
{
var result;
result = str.replace(/(^\s+)|(\s+$)/g,"");
if(is_global.toLowerCase()=="g")
{
result = result.replace(/\s/g,"");
}
return result;
}
/**
* 空格输入去除
* @param e
* @returns {Boolean}
*/
function inputSapceTrim(e,this_temp)
{
this_temp.value = Trim(this_temp.value,"g");
var keynum;
if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
if(keynum == 32){
return false;
}
return true;
}
/**
* 禁止空格输入
* @param e
* @returns {Boolean}
*/
function banInputSapce(e)
{
var keynum;
if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
if(keynum == 32){
return false;
}
return true;
}