Home >Web Front-end >JS Tutorial >Method of splitting names based on jquery (pure JS version)_jquery

Method of splitting names based on jquery (pure JS version)_jquery

WBOY
WBOYOriginal
2016-05-16 17:34:171034browse

I have previously shared a jquery plug-in that automatically splits the name into the first and last name in the form using js after the user enters the name in the dom. Due to the needs of the project, a jquery plug-in is needed to automatically split the name on the client, but the split result does not need to be presented. method for users, so I wrote an independent method and posted it to share with everyone

Copy the code The code is as follows:

$.extend({
splitName: function(fullname){
var hyphenated = ['Ouyang','Taishi','Duanmu','Shangguan','Sima', 'Dongfang','Dugu','Nangong','Wanqi','Wenren','Xiahou','Zhuge','Yuchi','Gongyang','Helian','Tantai',' Huangfu',
'Zongzheng', 'Puyang', 'Gongye', 'Tai Shu', 'Shentu', 'Gongsun', 'Murong', 'Zhongsun', 'Zhongli', 'Changsun', 'Yuwen','Chengchi','Situ','Xianyu','Sikong','Ruyan','Luqiu','Ziche','Qiguan',
'Sikou','Wuma ', 'Gongxi', 'Zhuansun', 'Rangsi', 'Gongliang', 'Qidiao', 'Lezheng', 'Zaifu', 'Guliang', 'Tuoba', 'Jiagu', 'Xuanyuan', 'Linghu', 'Duanqian', 'Baili', 'Huyan', 'Dongguo', 'Nanmen',
'Sheep's tongue', 'Weisheng', 'Gonghu', 'Gongyu', 'Gongyi', 'Liangqiu', 'Gongzhong', 'Gongshang', 'Gongmen', 'Gongshan', 'Gongjian', 'Zuoqiu', 'Gongbo', 'Ximen', 'Gongzu', 'Fifth', 'Gongcheng', 'Guanqiu', 'Gongxi',
'Nanrong', 'Dongli', 'Donggong', 'Zhongchang' ,'Zishu','Zisang','Jimo','Daxi','Chushi'];
var vLength = fullname.length;
var lastname = '', firstname = '';/ /The first is the surname, the last is the first name
if(vLength > 2){
var preTwoWords = fullname.substr(0, 2);//Get the first two words of the name to see if they are in the compound surname library
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 has only two characters, the previous one is Last name, the last name is the first name
lastname = fullname.substr(0, 1);
firstname = fullname.substr(1);
}else{
lastname = fullname;
}
return [lastname, firstname];
}
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn