Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change form entry

    I have a form with firstname in one field and lastname is another field.

    I need to manipulate the entry so if someone enters Joe for the firstname and Smith for the second name it will be changed to Smith,Joe.

    Here is my attempt but it just switched the fields and didnt manipulate how I wanted it:

    Code:
    var name = "Joe Smith"
    var pattern = /^([a-z-]+)\s+([a-z-']+)$/gi;
    name = name.replace(pattern, "$2,$1");
    name = name.replace(/,/, " ");

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Ummm...if first and last names are in SEPARATE fields, then WHERE do you want to put the new combined name??? And why would you ever put them together in the wrong way, as you seem to have done there, instead of just starting out right??

    And not to ask a silly question, but why did you do the second replace call, to get rid of the comma that the first call put in there????

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Agreed. But he *said* it was two fields.

    But even so, assuming a single field, I don't understand the point of the
    Code:
    name = name.replace(/,/, " ");
    line. All that does is get rid of the comma that was just put in there.

    Oh...and what happens if the name is "Mon'skij O'Brien"? <grin/>

    Seems to me a more sensible pattern might be simply:
    Code:
    var pattern = /^\s*([^\s]+)\s+([^\s]+\s*$/i; // should *NOT* be /g!
    Or, of course, use word/non-word instead of non-space/space.

    Finally, what does he do in the case that somebody enters *THREE* (or more) names?
    Dwight David Eisenhower
    George Herbert Walker Bush
    I'm a troublemaker.

    How about this code, instead?
    Code:
    var name = "    George      Herbert         Walker         Bush      ";
    name = name.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s{2,}/," ");
    var lastSpace = name.lastIndexOf(" ");
    name = name.substring(lastSpace+1) + ", " + name.substring(0,lastSpace);
    Utterly untested, as usual. But must be close.
    Last edited by Old Pedant; 06-15-2009 at 07:31 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    Agreed. But he *said* it was two fields.
    Phew, you are quick! You replied to my post which was up only a minute before I deleted it as not adding to yours.

    How about this code, instead?
    Code:
    var name = "    George      Herbert         Walker         Bush      ";
    name = name.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s{2,}/," ");
    var lastSpace = name.lastIndexOf(" ");
    name = name.substring(lastSpace+1) + ", " + name.substring(0,lastSpace);
    Utterly untested, as usual. But must be close.
    A small change:-

    name = name.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s{2,}/g," ");

    Then it works just fine! Also tested with Mon'skij O'Brien and Jean-Paul O'Flanagan-Clancy.
    Last edited by Philip M; 06-15-2009 at 07:48 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    oops...my face is red.

    Fun stuff. Will be interesting to hear what the real story is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •