为表单字段添加后缀 [英] Add suffix to form field

查看:111
本文介绍了为表单字段添加后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(超过一千个)用户坚持使用他们的名字登录,但系统坚持提供完整的电子邮件地址(名称+ @ my-mail.com)。



表单:



<$ p这是一个聪明的解决方案,可以在不影响用户的情况下添加后缀? $ p> < form id =login_formaction =processing.phpmethod =postclass =form>
< fieldset>
< ul class =input_block>
< li>
< input type =textname =emailid =emailvalue =/>
< / li>
< li>
< input type =passwordname =passwortid =password/>
< / li>
< / ul>
< / fieldset>



我玩过钥匙但是它并没有多大帮助。

 < script> 
$('#email')。keyup(function(e){
if(this.value.length> 12){
this.value = this.value +'@my ('@ my-mail.com')< = 0){
this.value = String.fromCharCode(e.which)+ '@ my-mail.com';
}
});
< / script>

我认为一个解决方案可以在提交之前操作字段,更加适当无法访问正在接收提交的PHP文件)。所以我也尝试了提交功能,这也没有效果。

 < script> 
$(#login_form).submit(function(event){
(#email)。value =(#email)。value +'@ my-mail.com';
});
< / script>

有人建议如何使用提交函数或另一个似乎更好的想法解决它?

解决方案

  $('#email')。change(function(){$ b $ val $ =(this).val(); 
if(val.indexOf('@ my-mail.com')== -1)
$(this).val(val + '@ my-mail.com');
});

http://jsfiddle.net/g4oLtfw7/



如果这个后缀还不是@ my-mail.com的后缀输入值。如果您想允许其他类型的电子邮件,但是默认为'my-mail.com',请尝试以下操作:

  $ ('#email')。change(function(){
var val = $(this).val();
if(val.indexOf('@')== -1)
$(this).val(val +'@ my-mail.com');
});


I have some (more than thousand) users that insist on logging in just with their names, but a system that insists on having the full e-mail address (name + @my-mail.com) provided. Which is a smart solution to add a suffix to a field without bothering the user?

The Form:

<form id="login_form" action="processing.php" method="post" class="form">
  <fieldset>
     <ul class="input_block">
        <li>
           <input type="text" name="email" id="email" value="" />
        </li>
        <li>
           <input type="password" name="passwort" id="password" />
        </li>
     </ul>
  </fieldset>

I played around with the key up function for the field, but it didn't help much.

<script>
$('#email').keyup(function(e){
   if(this.value.length > 12){
     this.value = this.value + '@my-mail.com';
  if( this.value.indexOf('@my-mail.com') <= 0 ){ 
     this.value = String.fromCharCode(e.which) + '@my-mail.com'; 
  }
});
</script>

I consider a solution that manipulates the field just right before the submission much more "proper" (sadly I don't have access to the PHP file that is receiving the submission). So I tried as well with the submit function, this didn't work either.

<script>
$( "#login_form" ).submit(function( event ) {
   ("#email").value = ("#email").value + '@my-mail.com';
});
</script>

Anybody some advise on how to solve it using the submit function or another idea that seems to be better?

解决方案

$('#email').change(function() {
  var val = $(this).val();
  if(val.indexOf('@my-mail.com') == -1)
    $(this).val(val+'@my-mail.com');
});

http://jsfiddle.net/g4oLtfw7/

This will add the '@my-mail.com' suffix if it's not already part of the input value. If you want to allow other types of emails, but default to 'my-mail.com' otherwise, try this:

$('#email').change(function() {
  var val = $(this).val();
  if(val.indexOf('@') == -1)
    $(this).val(val+'@my-mail.com');
});

这篇关于为表单字段添加后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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