MktoForms2 :: Fake Full Name

by sanford

HTML

<script src="//app-sj01.marketo.com/js/forms2/js/forms2.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<form id="mktoForm_169"></form>
<script>MktoForms2.loadForm("//app-sj01.marketo.com", "410-XOR-673", 169,
function(form) 
    {
    
    // handles to our interesting HTML elements
    var formEl = form.getFormElem()[0];
    var firstNameField = formEl.querySelector('INPUT[name="FirstName"]');
    var firstNameLabel = formEl.querySelector('LABEL[for="FirstName"]');

    // our new FullName field will only exist in the browser; it will not be posted to Marketo. 
    var fullNameField = firstNameField.cloneNode();             
    fullNameField.name = "FullName";
    fullNameField.id = "FullName";
    
    // insert the FullName field; reuse the label from the FirstName
    firstNameLabel.innerHTML = firstNameLabel.innerHTML.replace("First Name", "Full Name");
    firstNameField.parentNode.insertBefore(fullNameField,firstNameField);    
    
    // and hide the (real) FirstName 
    firstNameField.style.display = "none";
    
    // NOW link to Yanir Calisar's full name splitter function 
    splitFullName("FirstName","LastName","FullName");
        
 });        
</script>

JavaScript

function splitFullName(a,b,c){
                String.prototype.capitalize = function(){
                        return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
                };
                document.getElementsByName(c)[0].oninput=function(){
                        fullName = document.getElementsByName(c)[0].value;
                        if((fullName.match(/ /g) || []).length ===0 || fullName.substring(fullName.indexOf(" ")+1,fullName.length) === ""){
                                first = fullName.capitalize();;
                                last = "null";
                        }else if(fullName.substring(0,fullName.indexOf(" ")).indexOf(".")>-1){
                                first = fullName.substring(0,fullName.indexOf(" ")).capitalize() + " " + fullName.substring(fullName.indexOf(" ")+1,fullName.length).substring(0,fullName.substring(fullName.indexOf(" ")+1,fullName.length).indexOf(" ")).capitalize();
                                last = fullName.substring(first.length +1,fullName.length).capitalize();
                        }else{
                                first = fullName.substring(0,fullName.indexOf(" ")).capitalize();
                                last = fullName.substring(fullName.indexOf(" ")+1,fullName.length).capitalize();
                        }
                        document.getElementsByName(a)[0].value = first;
                        document.getElementsByName(b)[0].value = last;
                };
                //Initial Values
                if(document.getElementsByName(c)[0].value.length < 2 && document.getElementsByName(b)[0].value.length.length >2 && document.getElementsByName(a)[0].value.length.length >2 ){
                        first = document.getElementsByName(a)[0].value.capitalize();
                        last = document.getElementsByName(b)[0].value.capitalize();
                        fullName =  first + " " + last ;
                       ...