Html5 automatically implements email domain name completion for the text box

Source: Internet
Author: User
Tags gmail mail

Product Feedback users always enter the wrong email domain name, so the email address obtained by the server is incorrect. For example, there are many users who always input a xxx@qq.com into a xxx@qq.con.

This worries the backend, because the email address is legal, but the error is so obvious (I do not know whether the qq. con domain name exists ).

To solve this problem, I spent an hour in the afternoon using the new list attribute in html5 input. When the user inputs '@', the user is prompted with Domain Name Information. :

However, it was found that this property was not supported in safari, and thus could not be supported in the App. It is estimated that it can only be used in chrome. It's just learning. The general idea is to listen to the user input '@', construct a datalist, and associate it with the input. Dependent on jquery.

Copy to ClipboardReference: [www.bkjia.com] (function (){
Var emails = [{domain: '@ qq.com', label: 'qq mailbox '},
{Domain: '@ 163.com', label: 'mailbox 163 '},
{Domain: '@ 126.com', label: '1970 mailbox '},
{Domain: '@ hotmail.com', label: 'hotmail '},
{Domain: '@ sina.com', label: 'sina '},
{Domain: '@ gmail.com', label: 'gmail mail '},
{Domain: '@ 139.com', label: '123456 '},
{Domain: '@ yahoo.com.cn', label: 'yahoo China mailbox '}];

Var addEmailHelp = function (id, otherAddress ){
Var inputObj = $ ('input # '+ id );
If (inputObj. length = 0 ){
Return;
}

InputObj. keyup (function (ev ){
Var val = $ (inputObj). val ();
Var lastInputKey = val. charAt (val. length-1 );

If (lastInputKey = '@'){
Var indexOfAt = val. indexOf ('@');
Var username = val. substring (0, indexOfAt );

If ($ ('datalist # emaillist'). length> 0 ){
$ ('Datalist # emaillist'). remove ();
}

$ (InputObj). parent (). append ('<datalist id = "emailList"> </datalist> ');
For (var I in emails ){
$ ('Datalist # emaillist '). append ('<option value = "' + username + emails [I]. domain + '"label ="' + emails [I]. label + '"/> ');
}

If (otherAddress! = Null & typeof otherAddress! = 'Undefined '){
For (var I in otherAddress ){
$ ('Datalist # emaillist '). append ('<option value = "' + username + otherAddress [I]. domain + '"label ="' + otherAddress [I]. label + '"/> ');
}
}

$ (InputObj). attr ('LIST', 'emaillist ');
}
})
};

Window. addEmailHelp = addEmailHelp;
})();

You can set an id for input and associate it with addEmailHelp (id, otherAddress). This function is already associated with some frequently used mailbox domain names by default.

OtherAdress can be associated with other custom addresses. This parameter can be blank when a function is called.

An otherAddress instance: [{domain: '@ xxx.com', label: 'xxx mailbox '}, {domain:' @ yyy.com ', label: 'yyy mailbox'}].

A complete call instance:

<Html> 

Tip: the code can be modified before running!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.