姓名拆分姓和名

html Code:html

    <input type="text" name="myname" />
    <input type="button" id="cn_change" value="轉中文姓和名" />
    <input type="button" id="en_change" value="轉拼音姓和名" />
    <input type="button" id="en_change_no_space" value="轉拼音姓和名無空格" />
    <br/>
    姓:<input type="text" name="last_name" />
    名:<input type="text" name="first_name" />

Js Code(注:若使用中文轉拼音需引入jQuery.Hz2Py-min.js插件,網上可下載):數組

拆分姓和名函數函數

    //獲取姓和名
    function Get_LastName_And_FirstName(full_name){
        
        var hyphenated = ['歐陽','太史','端木','上官','司馬','東方','獨孤','南宮','万俟','聞人','夏侯','諸葛','尉遲','公羊','赫連','澹臺','皇甫',  
                          '宗政','濮陽','公冶','太叔','申屠','公孫','慕容','仲孫','鍾離','長孫','宇文','城池','司徒','鮮于','司空','汝嫣','閭丘','子車','亓官',  
                          '司寇','巫馬','公西','顓孫','壤駟','公良','漆雕','樂正','宰父','穀梁','拓跋','夾谷','軒轅','令狐','段幹','百里','呼延','東郭','南門',  
                          '羊舌','微生','公戶','公玉','公儀','梁丘','公仲','公上','公門','公山','公堅','左丘','公伯','西門','公祖','第五','公乘','貫丘','公皙',  
                          '南榮','東里','東宮','仲長','子書','子桑','即墨','達奚','褚師'];  
        
        var last_name = "";
        var first_name = "";
        
        //先判斷是否全爲英文字母 - 若全爲英文字母,姓爲2個字母。若中英參和或全爲中文 - 驗證是否存在兩名姓, 存在則姓2個字。
        if (full_name.search(/[A-Za-z]/) != -1) {  //中文姓名全爲英文
            
            last_name = full_name.substr(0,2);
            first_name = full_name.substr(2);
            
        } else {    //驗證是否存在    hyphenated數組中
            
            if(full_name.length > 2 ){
                var preTwoWords = full_name.substr(0,2);
                if($.inArray(preTwoWords, hyphenated) > -1){  
                    last_name = preTwoWords;
                    first_name = full_name.substr(2);
                }else{
                    last_name = full_name.substr(0,1);
                    first_name = full_name.substr(1);
                }
            }else{
                last_name = full_name.substr(0,1);
                first_name = full_name.substr(1);
            }
        }
        
        return {
            'last_name':last_name,
            'first_name':first_name
        };
    }

轉姓和名spa

$("#cn_change").on('click',function(){

        var myname = $("input[name='myname']").val();
        var name_arr = Get_LastName_And_FirstName(myname);
        var lastName = name_arr['last_name'];
        var firstName = name_arr['first_name'];

        $("input[name='last_name']").val(lastName);    //歐陽
        $("input[name='first_name']").val(firstName);    //娜娜
         
});

轉拼音姓和名插件

$("#en_change").on('click',function(){
        var myname = $("input[name='myname']").val();
        var name_arr = Get_LastName_And_FirstName(myname);
        var lastName = name_arr['last_name'];
        var firstName = name_arr['first_name'];

        $("input[name='last_name']").val(lastName);    //歐陽
        $("input[name='first_name']").val(firstName);    //娜娜
        $("input[name='last_name']").val($("input[name='last_name']").toPinyin().toLocaleUpperCase());    //OU YANG 
        $("input[name='first_name']").val($("input[name='first_name']").toPinyin().toLocaleUpperCase());    //NA NA 

});

轉拼音姓和名無空格分隔code

$("#en_change_no_space").on('click',function(){
        var myname = $("input[name='myname']").val();
        var name_arr = Get_LastName_And_FirstName(myname);
        var lastName = name_arr['last_name'];
        var firstName = name_arr['first_name'];

        $("input[name='last_name']").val(lastName);    //歐陽
        $("input[name='first_name']").val(firstName);    //娜娜
        $("input[name='last_name']").val($("input[name='last_name']").toPinyin().toLocaleUpperCase().replace(/ /gm,''));    //OUYANG 
        $("input[name='first_name']").val($("input[name='first_name']").toPinyin().toLocaleUpperCase().replace(/ /gm,''));    //NANA 

});
相關文章
相關標籤/搜索