1

So I want to create a prompt that asks for a clients name. I want to make sure it is formatted in this way: "Last name, first name, middle initial" and no name longer than 20 characters. Here is what I have so far, but not sure where to go from here.

<script language="JavaScript">
//Get the name
var name = prompt("What is your Name?");

//validate
if(name)
//output
document.write ("That is your name! :)");

else
document.write ("Please use the format Last name, first name, middle initial");
 </script>
3
  • 2
    First, using document.write is usually not a good idea. Also, is the 20 character limit based on the full name or each of the parts? What happens if someone does not have a middle name or surname? Commented Dec 13, 2013 at 4:05
  • 2
    Client side validation is fine but make sure to have server side validation as well. Clients could disable javascript. Commented Dec 13, 2013 at 4:06
  • the prompt is just for testing. actually it is supposed to be a max of 20 characters per name Commented Dec 13, 2013 at 4:54

3 Answers 3

4

Have a look at the below simple steps using regular expression :

// Get name
var name = prompt("What is your Name?");

// match name with regular expression
if (name.match("^[A-Za-z]{1,20}, [A-Za-z]{1,20}, [A-Za-z]{1,20}")) {
    document.write("That is your name! :)");
} else {
    document.write("Please use the format Last name, first name, middle initial");
}

Result :

Match :

  1. abc, xyz, m ,
  2. abcabcabc, xyzzyz, mr

Non Matchs :

  1. , xyz, m ,
  2. 123, a, b,
  3. iammorethantwentycharactersfirstname, middle, initial
Sign up to request clarification or add additional context in comments.

1 Comment

The last regex part should be [A-Za-z]{1} because the OP is just looking for a middle name initial, but +1 for the explanation
0

To exactly fit your criteria:

var s = 'smith, john, j';
var isValid = /^\w+, \w+, \w$/.test(s) && s.replace(/[, ]/g,'').length < 21;

but that is very specific and assumes the 20 character limit applies to the total of the parts of the name and doesn't inlcude any spaces, commas, etc. BTW, the above will fail for hyphenated names so the regular expression might need to be:

/^[\w-]+, [\w-]+, \w$/;

to allow say 'Smith-Jones, Mary-Jo, A'.

4 Comments

This seems to be what I am looking for, but how do I use that validation to pass a valid or invalid message to the output?
@user3097967: isValid is a boolean, use that for your if condition. And I'll ask again: is the 20 character limit based on the full name or each of the parts?
Hmm not sure I am getting it. Here is what I have (obviously wrong) <script language="JavaScript"> //Get the name var name = prompt("What is your Name?"); //validate var isValid = /^\w+, \w+, \w$/.test(s) && s.replace(/[, ]/g,'').length < 21; if(name isValid) { document.write ("Invalid name"); } else{ //output document.write ("Valid name"); } </script>
Just if (isValid) ....
0

Your code should be like:

<script language="JavaScript">
    //Get the name
    var name = prompt("What is your Name?");

    //validate
    if(name && name.length <=20)
    {
        var i = name.split(',');
        if(i.length<3){
            document.write ("Please use the format Last name, first name, middle initial");
        }else{
            //output
            document.write ("Your name is: " +name);
        }
    }else{
        document.write ("Please use the format Last name, first name, middle initial");
    }
 </script>

But this is a not good design, because like what @Qantas94Heavy mention in your comment, if a user doesn't have a middle name or last name, your form will make it difficult for them to sign up.

Note: Also this is just a basic validation to make sure that the name has last name, first name and middle initial and the name is not longer than 20 characters as the OP mentioned. Of course if you want to be more accurate and advance you can use Regex to validate it again.

8 Comments

name.length >=20 how does it make sure that each name is not longer than 20 characters ?
He only mention name but not each name, assuming from his codes, he only has one input. AND if you think about it, will you have first name, middle name and last name with 20 characters each? I am quite amazed I got downvote for that.
name.length >=20 will check for the at least 20 characters. if my firstname is 25 characters long, lastname is 30 characters long and intial is 15 characters then your condition will become true instead of false.
This doesn't seem to work, no matter how I format the name it comes back with the "Please use the format Last name, first name, middle initial" message. Am I missing something?
Qantas94Heavy and @SpiderCode, apologised for not checking it throughly. Apparently my finger miss a few cm and hit the wrong key. edited my answer. Thanks for point it out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.