无法修剪电子邮件输入中的空格 [英] Cannot trim whitespace on email input

查看:115
本文介绍了无法修剪电子邮件输入中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修剪电子邮件输入中的尾随空白,但是认为修剪应用于电子邮件输入时存在问题.谁能解释为什么会这样,或者我的假设是否不正确.当应用此代码时,它适用于密码字段,而不适用于电子邮件字段.

I was trying to trim trailing whitespace on an email input but believe there is an issue with trim being applied to email inputs. Can anyone explain why this is the case or whether I am incorrect in my assumptions. This code when applied works on the password field but not on the email field.

这是Ruby on Rails应用程序,但是创建的HTML格式如下:

It is a Ruby on Rails application but the HTML created is like so:

查看:

<input class="mandatory form-control form-control" id="user_email" maxlength="250" name="user[email]" required="required" type="email">
<input class="mandatory form-control form-control" id="user_password" maxlength="20" name="user[password]" required="required" type="password">

jQuery:

$('input').blur(function()
{
    this.value=$(this).val().trim();
});

我也尝试过这种方法,但是还是没有运气.它会在文本之间去除空格,但仍不会删除结尾的空格:

I have also tried this but still no luck. It strips whitespace between text but still wont remove trailing whitespaces:

$('input[type=email]').on('blur', function () 
{
    $(this).val($(this).val().replace(/ /g,''));
})

推荐答案

我发现的最佳方法是:

$(function()
{
  $('input[type=email]').on('keypress', function(e)
  {
    if (e.which == 32)
      return false;
  });
});

所有其他方法都会导致输入类型电子邮件的异常行为,从而导致仍然显示空格.这种方法可以停止显示空格,并且因为电子邮件中不应包含任何希望的空格.

All other approaches lead to strange behavior for input type email that lead to spaces still being presented. This approach stops the space appearing and because an email shouldnt have any spaces it works as hoped.

这篇关于无法修剪电子邮件输入中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆