I have a field in my form labeled ‘Name’ that will contain both the First & Last name.
Our existing dynamic server (to which the form is being POSTed to), expects two separate fields (first name, last name).
Can I use Javascript to split the user input into two separate variables before the form is posted to the server? How would I do this?
I would process this on the server end to make sure the data that is passed is accurate from what was posted. It’s relatively easy programmatically to split the name, but the problem is how do you define your separator. Most would agree to split it wherever there is a white-space character, but what if they enter a three word name such as Don Juan DiMarco? If you decide to split the name based on whitespace, you’ll have to determine how to assign the first and last names.
The above will give you (in PHP) an array of values split by white space. Running that on the string Don Juan DiMarco would give you an array like:
From there you have to determine which ones are the first name, and which are a middle, and which are a last name. It gets even harder if you have 4 or 5 names entered, which is entirely realistic for certain cultures. All of this is guesswork, which is why I would recommend simply adding a Last Name input field on the front-end. This would eliminate all guess work as to which name is the first name and which is the last name.