Make it possible for Hubspot Forms to separate First/Last Name from one Field

Status: Not Currently Planned

Status history

I’m not sure if this has already been resolved elsewhere… but i’ve written a little bit of scripting that achieves just this… It’s also accounting for various prefixes for last names (Mc, Mac, Von, etc.).

Basically, what i’ve done is created a first, middle and last name field and hidden them within the forms, then added an onsubmit callback that processes the full name and splits it up into the first / middle / last properties. So far it seems to be working rather well…

The code for splitting the name…

function capitalizeFirstLetter(str) {
 return str.charAt(0).toUpperCase() + str.slice(1);
}

function checkPrefixes(str, arr){
 for(var i = 0; i < arr.length; i++)
 if (str.toLowerCase() === arr[i].toLowerCase()){
 return true;
 }
 return false;
}

function separateNames($form){
 var namePrefixes, fullName, splitName, user;

 namePrefixes = ["Ab","Af","Ap","Abu","Aït","Al","Ālam","Aust","Austre","Bar","Bath","bat","Ben","bin","ibn","Bet","Bint","Da","Das","De","Degli","Dele","Del","Della","Der","Di","Dos","Du","El","Fetch","Vetch","Fitz","Kil","Gil","La","Le","Lille","Lu","Mac","Mc","Mck","Mhic","Mic","Mala","Mellom","Myljom","Na","Ned","Nedre","Neder","Nic","Ni","Nin","Nord","Norr","Ny","Ua","Ui","Opp","Upp","Öfver","Ost","Öst","Öster","Øst","Øst","Østre","Över","Øvste","Øvre","Øver","Öz","Pour","Stor","Söder","Ter","Ter","Tre","Van","Väst","Väster","Verch","Erch","Vest","Vesle","Vetle","von","zu"];

 user = {
 firstName: '',
 middleName: '',
 lastName: ''
 }
	console.log('maybe.....' + user);
 fullName = $form.find('input[name="full_name"]').val();
 fullName = fullName.replace(/\s+/g, ' ').trim();
 splitName = fullName.split(" ");
	console.log('we found the field value!!');

if(splitName.length === 1){
 user.firstName = splitName[0];
}

if (splitName.length === 2) {
 user.firstName = splitName[0];
 user.lastName = splitName[1];
}

if (splitName.length === 3) {
 user.firstName = splitName[0];
 user.middleName = splitName[1];
 user.lastName = splitName[2];
}

if (splitName.length >= 4){
 user.firstName = splitName[0];

 for (var i = 1; i < splitName.length; i++){

 if (checkPrefixes(splitName[i], namePrefixes) === false && i !== (splitName.length - 1) && user.lastName === ""){
 user.middleName += splitName[i] + " ";
 }

 else {
 user.lastName += splitName[i] + " ";
 }
 }
}

if (user.middleName.length < 3 && user.middleName.indexOf('.') !== -1) {
 user.lastName = user.middleName + user.lastName;
 user.middleName = '';
}

user.firstName = capitalizeFirstLetter(user.firstName);
user.middleName = capitalizeFirstLetter(user.middleName);
user.lastName = capitalizeFirstLetter(user.lastName);

console.log("the full name is ... \n First: " + user.firstName + "\n Middle: " + user.middleName+ "\n Last: " + user.lastName);

return user;

}
function injectFieldData(el, data){
 var inputField = document.querySelector(el) || null; 
 if(inputField !== null){
 if(data !== undefined){
 inputField.value = data;
 }
 }
}

function postFormSubmission($form){
 var user = separateNames($form);
 console.log(user);
 $form.find('input[name="firstname"]').val(user.firstName).change();
 $form.find('input[name="middlename"]').val(user.middleName).change();
 $form.find('input[name="lastname"]').val(user.lastName).change();
}

and then when you are creating a form, via the api, pass in the onFormSubmit…

hbspt.forms.create({
 portalId: hsForm.portalId || "your portal ID",
 formId: hsForm.formId,
 target: hsForm.target || this,
 cssClass: hsForm.style || "hsForm",
 	onFormReady: hsForm.onFormReady || '',
	 onFormSubmit: function($form){
 			console.log($form);
			 postFormSubmission($form);
 },
 });

I hope that this helps you out…