gpt4 book ai didi

javascript - 消除输入字段中的空白

转载 作者:行者123 更新时间:2023-12-03 08:49:03 24 4
gpt4 key购买 nike

当用户在电子邮件字段中输入电子邮件时,我很难尝试摆脱空白(通过按空格键创建),我该怎么做......请参阅下面的代码......我想摆脱通过空格键输入电子邮件后删除空格,或者我如何在单击提交按钮后立即删除这些空格!,提前感谢...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>
<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>

最佳答案

当用户使用输入时,您可以“阻止”空间: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>

关于javascript - 消除输入字段中的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32762337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com