Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  81] [ 3]  / answers: 1 / hits: 14756  / 11 Years ago, fri, december 13, 2013, 12:00:00

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>

More From » html

 Answers
0

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


[#49574] Thursday, December 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gavenmekhio

Total Points: 732
Total Questions: 89
Total Answers: 93

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
;