Name Splitting Method Based on jquery (JS version only)

Source: Internet
Author: User

I have already shared a jquery plug-in the dom that is automatically split into a surname and a name into a form using js after the user enters the name. Due to project requirements, a client is required to automatically split the file, however, the splitting result does not need to be presented to the user, so I wrote an independent method and posted it to share it with you.
Copy codeThe Code is as follows:
$. Extend ({
SplitName: function (fullname ){
Var hyphenated = ['ouyang ', 'taishi', 'duan Mu ', 'shangguan', 'sima ', 'dongfang', 'lone out', 'nangong', 'wan ', 'wen Ren ', 'xia hou', 'zmag', 'yun Chi', 'goat ', 'helian', 'tai Tai ', 'huang Fu ',
'Zheng Zheng', 'puyang ', 'gong ye', 'tai Shu', 'shentu ', 'gongsun', 'murong ', 'zhong sun', 'zhongli ', 'Great Sun', 'yuwen', 'chengchi ', 'situ', 'Fresh out', 'sikong', 'ruby', 'zhuqiu', 'ziche ', 'Administrative authorization ',
'Scol', 'witch m', 'gongxi', 'grandsun ', '', 'maleli', 'signiao ', 'lechen ', 'slaughter father ', 'Gu Liangliang', 'taobao', 'clip Gu ', 'xuanyuan', 'ling hu ', 'duan Gan', 'baili ', 'hexten ', 'dongguo ', 'nammen ',
'Goat tongue ', 'microsense', 'public household', 'gong Yu ', 'gong Yi', 'liangqiu ', 'gong Zhong', 'gong Shang ', 'Gateway', 'mountain ', 'gost', 'zuozugu', 'gong Bo', 'ximen ', 'gong zu', '5 ', 'Public pass', 'guanqiu ', 'Fair Z ',
'Cloud', 'dongli ', 'donggong', 'zhong Chang', 'zi Shu ', 'zi san', 'jimo', 'Da taobao ', 'authorization'];
Var vLength = fullname. length;
Var lastname = '', firstname =''; // The name is the first and last name.
If (vLength> 2 ){
Var preTwoWords = fullname. substr (0, 2); // obtain the first two words of the name to check whether the name is in the database.
If ($. inArray (preTwoWords, hyphenated)>-1 ){
Lastname = preTwoWords;
Firstname = fullname. substr (2 );
} Else {
Lastname = fullname. substr (0, 1 );
Firstname = fullname. substr (1 );
}
} Else if (vLength = 2) {// when the full name is only two characters, the previous one is the last name and the next one is the name
Lastname = fullname. substr (0, 1 );
Firstname = fullname. substr (1 );
} Else {
Lastname = fullname;
}
Return [lastname, firstname];
}
});

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.