简体   繁体   English

如何在电子邮件地址输入字段中显示默认域名?

[英]How to show a default domain name in an email address input field?

I have a login form for a website that requires email address and password. 我有一个需要电子邮件地址和密码的网站登录表单。 Most users have the same email address domain, but a few users have email addresses from a different domain. 大多数用户具有相同的电子邮件地址域,但少数用户具有来自不同域的电子邮件地址。

Is there a way to automatically use a default domain name in the email address input field, while still allowing the user to type in a different domain name? 有没有办法在电子邮件地址输入字段中自动使用默认域名,同时仍然允许用户键入不同的域名?

For example, if | 例如,如果| is the cursor, the input would look something like this while typing: 是光标,键入时输入看起来像这样:

|@defaultreallylongdomain.com (user hasn't typed anything yet) |@defaultreallylongdomain.com (用户尚未输入任何内容)

b|@defaultreallylongdomain.com b|@defaultreallylongdomain.com

bo|@defaultreallylongdomain.com bo|@defaultreallylongdomain.com

bob|@defaultreallylongdomain.com bob|@defaultreallylongdomain.com

bob@|defaultreallylongdomain.com bob@|defaultreallylongdomain.com

bob@g| 鲍勃@ G |

.

. (user keeps typing) (用户不断输入)

.

bob@gmail.com| bob@gmail.com |

The default domain shows up to the right of the cursor, until the user starts typing a domain name that doesn't match the default domain. 默认域显示在光标右侧,直到用户开始键入与默认域不匹配的域名。

Any ideas on how to accomplish this? 有关如何实现这一目标的任何想法?

Use selectionStart 使用selectionStart

Here is example: 这是一个例子:

html HTML

<input id='email' type='text'>

js JS

const input = document.getElementById('email');
const defaultEmail = '@gmail.com';
input.value = defaultEmail;

function handleInput() {
    const val = input.value;
    const sel = input.selectionStart;
    if (val.length - defaultEmail.length < sel) {
        input.value = val.slice(0, sel)
        input.removeEventListener('input', handleInput);
        input.removeEventListener('focus', handleInput);
        input.removeEventListener('click', handleInput);
    }
}

input.addEventListener('input', handleInput);
input.addEventListener('focus', handleInput);
input.addEventListener('click', handleInput);

Live demo 现场演示

This is pretty inelegant and basic example, but you could build on it. 这是非常不优雅和基本的例子,但你可以建立它。 Essentially, you could; 基本上,你可以;

  1. Pre-fill the input with the default domain 使用默认域预填充输入
  2. Keep track of the currently typed email 跟踪当前键入的电子邮件
  3. Force the user to the start of the input when clicked 单击时强制用户开始输入
  4. If the user types a "@" character, you know they want a different domain 如果用户键入“@”字符,您知道他们想要一个不同的域
  5. Replace input value with what the user has typed so far 将输入值替换为用户到目前为止输入的内容

Like this; 像这样;

 $(document).ready(function(){ let defaultDomain = 'exampledomain.net'; // use this to remove later let input = $("#testInput"); let typed = ""; // keep track of what the user has typed // put cursor at start when clicked input.click(function(){ $(this).get(0).setSelectionRange(0,0); }) input.keydown(function(e){ // @ is keycode 192 if(e.keyCode === 192){ // the user has typed @ and you can assume that they want a different domain input.val(typed.substring(0, typed.length - 1)); }else{ // store the email the user has typed so far, minnus default domain typed = input.val().replace(defaultDomain, ''); } }) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="testInput" value="@exampledomain.net"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM