jQuery: Text Input Split Tutorial

This tutorial will show you how to take one input field and split the contents into two hidden fields, which is useful when you have limited space on a form. In my example, I have a field called Name, but I would like the value split into two fields and stored in the database in two different columns First Name and Last Name. Using JavaScript and a little simple trickery, we can make this happen.

To view a working demo, Click here.

  1. Setup the HTML form with the proper ID’s so that the fields can be easily selected with jQuery
      <title>jQuery Split</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
          <input name="name" id="name" type="text">
          <input name="first_name" id="first_name" type="text"/>
          <input name="last_name" id="last_name" type="text"/>
          <input type="submit" id="submit">
  2. Get the value of the #name field and set it equal to the var nameVal
    var nameVal = $('#name').val()
  3. Assign the length of the #name field to the var nameLength
    var nameLength = nameVal.length;
  4. Use the JS split method, to pull out the values contained in var nameVal assign to an array nameSplit
    var nameSplit = nameVal.split(" ");
  5. Subtract nameLength from the first item in the array and assign the value to var lastLength
    var lastLength = nameLength - nameSplit[0].length;
  6. Assign the length of the first item in the array + 1
    var lastNameLength = nameSplit[0].length + 1;
  7. Using the JS slice method, we will slice the nameVal, using the value from lastNameLength
    var lastName = nameVal.slice(lastNameLength);
  8. Select the #first_name and #last_name fields and change the values
  9. Create a click event using jQuery, and place all the proceeding code within this function so that when the submit button is clicked – the new values will be set.
    $(document).ready(function() {
        var nameVal = $('#name').val()
        var nameLength = nameVal.length;
        var nameSplit = nameVal.split(" ");
        var lastLength = nameLength - nameSplit[0].length;
        var lastNameLength = nameSplit[0].length + 1;
        var lastName = nameVal.slice(lastNameLength);
        return false;

5 thoughts on “jQuery: Text Input Split Tutorial”

  1. Line 08 of your code is never referenced and does not look needed. But regardless, couldn't you have just done the following?

    01 $(document).ready(function() {
    03 $('#submit').click(function(){
    05 var nameVal = $('#name').val()
    06 var nameSplit = nameVal.split(" ");
    08 $('#first_name').val(nameSplit[0]);
    09 $('#last_name').val(nameSplit[1]);
    11 return false;
    12 });

  2. thank you for posting this great article. how could I implement this during a jquery validate plugin process:

    $(document).ready(function($) {
    rules: {
    name: "required",
    email: {
    required: true,
    email: true
    messages: {
    name: "Please enter your name",
    email: "Please enter a valid email address",
    submitHandler: function(form) {
    $("button").attr('disabled', 'disabled');

  3. The jquery for giving split input is something that I have been looking for all over the internet I have been in vain so far until I found this one here. Thank you for sharing this informative post and I hope to hear more updates from you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: