从输入字段中消除空白区域

时间:2015-09-24 13:14:34

标签: javascript html

当用户在电子邮件字段中输入电子邮件时,我正在努力摆脱空白区域(通过按空格键创建),我怎么能这样做....请参阅下面的代码..我想摆脱在空格键输入电子邮件后关闭空白区域,或者如果我点击提交按钮后如何摆脱这些空格!,提前感谢... singhy



<script language="javascript" type="text/javascript">
function removeSpaces(string) {
 return string.split(' ').join('');
}
</script>
<script>
function IsEmpty(){
  if(document.forms['frm'].email.value == "")
  {
    alert("email address here");
    return false;
  }
    return true;
}
</script>
&#13;
<form name="frm" class="rform" method="post" action="form_acknowledge.php" >
<fieldset><legend><strong>test</strong></legend> 
<p>enter email address</p>
  
<p><label for="email">Email address:</label> <input title="Your email address." required name="email" type='email' pattern=".+(@gmail.com)|.+(@hotmail.co.uk)" required />
</p>

<input id="insert" onclick="return IsEmpty();" style="float: right;" onblur="this.value=removeSpaces(this.value);" type="submit" name="submit" value="Subscribe" />
</fieldset>

</form>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

当用户使用输入时,您可以“阻止”空间: jsfiddle:http://jsfiddle.net/darxide/oz73u4eg/

<input type="text" id="mail" />

<script type="text/javascript">
document.getElementById('mail').addEventListener('keydown' ,function(e) {
    var k =e.keyCode
    k ==32 &&e.preventDefault()
})
</script>

答案 1 :(得分:0)

您的问题出在blur事件中。您将它附加到按钮,模糊意味着当元素失去焦点时,因此必须在输入文本中而不是在按钮中。参见示例:

&#13;
&#13;
function removeSpaces(string) {
 return string.split(' ').join('');
}
function IsEmpty(){
  if(document.forms['frm'].email.value == "")
  {
    alert("email address here");
    return false;
  }
    return true;
}
&#13;
<form name="frm" class="rform" method="post" action="form_acknowledge.php" >
<fieldset><legend><strong>test</strong></legend> 
<p>enter email address</p>
  
<p><label for="email">Email address:</label> <input title="Your email address." required name="email" type='email' pattern=".+(@gmail.com)|.+(@hotmail.co.uk)" required onblur="this.value=removeSpaces(this.value);"/>
</p>

<input id="insert" onclick="return IsEmpty();" style="float: right;"  type="submit" name="submit" value="Subscribe" />
</fieldset>

</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

检查Map<T1, Map<T2, Map<T3, T4>>> javascript方法,删除开始和结束字符串之间的所有空格

答案 3 :(得分:0)

感谢所有帮助我的人,Marcos代码解决了问题,onblur被放在按钮上它本来应该在带有必需标签的输入字段上,见上面....感谢所有人,你们和gals都很棒,谢谢:+ )