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 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    validate that name has 2 names

    I have a name field -I want to validate that there is a space in the field - that the user has put in both firstname lastname and not just one

    can someone help me with this?

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Here ya go:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Example</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // <![CDATA[
    
    function validateMe(form_elm)
    	{
    	var full = form_elm.full_name.value;
    	// clear any spaces in front
    	while (full.charAt(0) == " ") full = full.substr(1);
    	// split and check for two names - dual purpose of also retrieving first and last name for future use
    	full = full.split(" ");
    	var first_name = full[0];
    	var last_name = full[1];
    	if (full.length < 2) alert("Please enter both your first and last name.");
    	else alert("Thank you "+first_name+" "+last_name+".");
    	return false;
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <form onsubmit="return validateMe(this)">
    	<input type="text" name="full_name" /><br />
    	<input type="submit" value="submit" />
    </form>
    
    </body>
    </html>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    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
    This script checks that the field contains two names, with a least two alpha (only) characters in the first name and three alpha (only) characters in the last name, allowing for hyphens and/or apostrophes in the name e.g. Smith-Jones, O'Reilly; strips any leading or trailing spaces; removes multiple spaces between the names; capitalises the first letter of each word as required in English, with remaing letters in lowercase.

    Code:
    <input type = "text" name = "fullname" id = "fullname" onblur = "chkFullName()">
    
    <script type = "text/javascript">
    function chkFullName() {
    var full = document.getElementById("fullname").value;
    full = full.replace(/^\s+|\s+$/g,"") ;  // strip any leading or trailing spaces
    full = full.replace(/\s+/g," ");  // remove multiple intermediate spaces
    full = full.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});  // capitalise first letter of each name
    if (!/(^[A-Za-z]{2,}\s[A-Za-z-']{3,}$)/.test(full)) {   // first and second names separated by a space, allow for hyphen and apostrophe
    alert ("You must enter your first name (not just your initial) and last name separated by a space");
    document.getElementById("fullname").focus();
    return false;
    }
    else {alert ("Thank you " + full) }
    }
    </script>


    When trouble arises and things look bad, there is always one individual who perceives a solution and is willing to take command. Very often, that individual is crazy. Look out! He's coming!
    Last edited by Philip M; 01-23-2009 at 07:44 AM. Reason: Improved

  • #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
    This post refers to a post by another person which has been deleted by the Moderators.

    Capitalizing the first letter of the names is meaningless for databases -- it isn't recognized.
    Who mentioned a database?

    You did not strip out any excess intermediate spaces, so
    Cran space space Ford is invalid.
    While C F-d and C F'd are both valid (the OP wanted firstname, not just initial). Hardly better than Cran Ford---
    Cran O'Ford-Midwest with both an apostrophe and a hyphen returns false, but true in my regex.

    So overall, not really a very great improvement, nicht wahr?



    "He was too clever by half. We all thought he would either end up in the Cabinet or in prison. We never dreamt he would do both."
    Last edited by Philip M; 01-24-2009 at 08:07 AM.

  • #5
    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
    This post refers to a further post by the same person which has been deleted by the Moderators.

    Test code, before you run your mouth. Never mind, you'll never be able to extend that simple courtesy to anyone.
    Funny, I did test the code:-

    Code:
    <script type = "text/javascript">
    var nUser = "Cran  Ford";
    if (!/^\s*[a-z]+\s{1}[a-z]+[-']{0,1}[a-z]+\s*$/i.test(nUser)) {
    alert ("Keep taking the pills!")
    }
    </script>
    A first name is a name, not an initial. So minimum two characters (Al or Jo).

    esthera - if you want to take account of Cranford's objections, you can add the following three lines to the code I provided:-

    Code:
    full = full.replace(/['-]+$/g,"?");  // no apostrophe or hyphen at end
    full = full.replace(/-.*-/g,"?");   // only one hyphen allowed
    full = full.replace(/'.*'/g,"?");   // only one apostrophe allowed
    Personally I would not bother. There is virtually no way to stop some clown entering his name as Xx Zzz. You can ensure that each name contains one vowel and one consonant (Byng or Lee), and you can erase rude words (but be careful - Pen-istone and Scun-thorpe without the hyphens are English towns). But at the end of the day you cannot prevent some joker from entering his name as Cranford Gobonk-Yourself.
    Last edited by Philip M; 01-24-2009 at 08:05 AM.

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    why would you make its so hard for him.

    Code:
    function nameCheck(text){
          name = text.split(' '); // make and array with [1] = fname [2] = ' ' [3] = lname
          if(name[3] == ''){
           alert('You Name is missing the last name');// tells them
           return false; // prevent form from submitting
          }else{
            return true;
          }
    }
    This is to be used as onSubmit in form tag used by 'nameCheck(docment.getElementByID('name').value);'

    Nice and simple. ok lacking in security but nice and simple
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #7
    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 barkermn01 View Post
    why would you make its so hard for him.

    Code:
    function nameCheck(text){
          name = text.split(' '); // make and array with [1] = fname [2] = ' ' [3] = lname
          if(name[3] == ''){
           alert('You Name is missing the last name');// tells them
           return false; // prevent form from submitting
          }else{
            return true;
          }
    }
    This is to be used as onSubmit in form tag used by 'nameCheck(docment.getElementByID('name').value);'

    Nice and simple. ok lacking in security but nice and simple
    Another "nice and simple" script posted without testing. It does not work.

    I don't understand why people post scripts which are so obviously defective and cobbled together without any attempt to test them. This can only lead to frustration and disappointment for OPs who are beginners. In spite of Cranford's ludicrous comments, I always test my scripts before posting them (which is not to say that they are incapable of improvement).


    If ALL that is wanted is to check that a space is entered (anywhere) into the input field, then:

    Code:
    <script type = "text/javascript">
    function chkFullName() {
    var full = document.getElementById("fullname").value;
    if (!/\s/.test(full)) {
    alert ("You must enter first name and last name");
    return false;
    }
    }
    </script>
    It seem me that such a "validaton" is barely worthy of the name. At the very minimum you need to strip leading and trailing spaces so that one or more spaces alone do not return true.
    Last edited by Philip M; 01-23-2009 at 12:32 PM.

  • #8
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    I think the solution is obvious: Have the form e-mail you each individual name. You can then spend your day making sure each name is full, polite and plausible before entering them into your system
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Posting Permissions

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