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];
}
});