I have this JavaScript exercise can not find solution on last part, this is what
ID: 3667305 • Letter: I
Question
I have this JavaScript exercise can not find solution on last part, this is what I need.
Input: A text string, using prompt.
Output: Either "Valid name" or "Invalid name", depending on whether the input names fit the required format, which is Last name, first name, middle initial.
Where neither of the names can have more than 15 characters.
This is what I got so far and it is working except last part either of the names can have more than 15 characters.
var name = prompt("Please enter (Last name, First name, Middle initial)");
function fullName(alf) {
var validate = alf.match(/^[a-zA-Z', ]+$/);
if (validate == name)
return true;
else
return false;
}
var tst = fullName(name);
if (tst)
document.write("Valid Name");
else
document.write("Invalid Name");
Explanation / Answer
you can use the string.length method to get the length of the method so you can write as follows
function fullName(alf) {
var validate = alf.match(/^[a-zA-Z', ]+$/);
if (validate == name && name.length<=15)
return true;
else
return false;
}