【发布时间】:2014-07-24 05:56:49
【问题描述】:
我正在做电子邮件验证;在电子邮件字段中,用户可以放置空格。
所以我决定$trim 输入值并重新分配给输入字段。
它可以正常工作,没有任何问题,但是当我使用空格键几次并从输入中聚焦并返回时,输入字段中输入的空格。
虽然我正在修剪每个 input 事件 - 我不知道为什么空间与 input 字段。
请问有什么正确的方法吗?
这是我的尝试:
$('#email').on('input change',function(){
this.value = $.trim(this.value); //re-assigning trimmed value, but not works when enterning space and focus out. when focus in the space being in the input
validateEmail.init(this.value);
if(validateEmail.done()){
console.log('this is correct');
}
});
//enter some space and focus out, then come back to input field you can see white spaces in the field
【问题讨论】:
-
真的需要去掉输入框中的每个空格吗?为什么在将其传递给您的电子邮件验证时不直接修剪它? (
validateEmail.init($.trim(this.value));) -
在 FF 中可以正常工作,但在 chrome 中不行
-
我想基本上避免空格,因为这是电子邮件地址。再次,用户也可以复制粘贴一些不需要的空白。它在输入字段中变得非常冗长。
-
现在代码已更新,现在它在开始时使用多个空格,最后它将删除所有空格试试这个...
标签: javascript jquery validation email trim