[英]Add suffix to form field
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. 我有一些(超过一千个)用户坚持只使用他们的名字登录,但是一个系统坚持提供完整的电子邮件地址(name + @ my-mail.com)。 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). 我考虑了一种解决方案,该解决方案在提交之前就更“适当”地操作字段了(很遗憾,我无权访问接收提交的PHP文件)。 So I tried as well with the submit function, this didn't work either. 因此,我也尝试使用了Submit函数,但此方法也不起作用。
<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? 有人建议如何使用Submit函数或其他更好的想法解决它吗?
$('#email').change(function() {
var val = $(this).val();
if(val.indexOf('@my-mail.com') == -1)
$(this).val(val+'@my-mail.com');
});
http://jsfiddle.net/g4oLtfw7/ http://jsfiddle.net/g4oLtfw7/
This will add the '@my-mail.com' suffix if it's not already part of the input value. 如果后缀“ @ my-mail.com”尚未包含在输入值中,则会添加该后缀。 If you want to allow other types of emails, but default to 'my-mail.com' otherwise, try this: 如果要允许其他类型的电子邮件,但默认为“ my-mail.com”,请尝试以下操作:
$('#email').change(function() {
var val = $(this).val();
if(val.indexOf('@') == -1)
$(this).val(val+'@my-mail.com');
});
Either: 或者:
$('#login_form').submit(function (e) {
var email = $('#email'),
user = email.val().split('@')[0],
domain = '@my-mail.com';
if (email.val().toLowerCase() !== (user + domain).toLowerCase()) {
email.val(user + domain);
}
});
or 要么
$('#email').change(function (e) {
var email = $(this),
user = email.val().split('@')[0],
domain = '@my-mail.com';
if (email.val().toLowerCase() !== (user + domain).toLowerCase()) {
email.val(user + domain);
}
});
is how I would approach this (obligatory fiddle: http://jsfiddle.net/e84v7nat/ ). 这就是我的处理方式(强制性提琴: http : //jsfiddle.net/e84v7nat/ )。
This approach ensures that your user has a domain specified and that the domain is correct. 这种方法可确保您的用户指定了一个域,并且该域正确。 If the username is case-sensitive, remove the calls to .toLowerCase
. 如果用户名区分大小写,请删除对.toLowerCase
的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.