1

I have been using a regex code for fullnames in JavaScript and the pattern looks like this : /(\w)\s(\w)/ . The problem is that this pattern accepts numbers which I don't won't in the users fullname. So,I need a pattern that accepts a space after the first name and accepts a second name after the space and has a limit of 30 characters but does not accept numbers. I don't know how to create a regex pattern so any help is appreciated.

This is what the code looks like now:

    if(name.match(/(\w)\s(\w)/)){
        flagOne = true;
    }
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Kaleb Meeks
  • 111
  • 1
  • 1
  • 9
  • 4
    see [regular-expression-for-first-and-last-name](http://stackoverflow.com/questions/2385701/regular-expression-for-first-and-last-name) – gaetanoM Jan 03 '17 at 21:35
  • 2
    Have a look at this question: http://stackoverflow.com/questions/2385701/regular-expression-for-first-and-last-name – Sam Kool Jan 03 '17 at 21:36

1 Answers1

1

You can use the following pattern to match only alphabetical characters (be aware, that you are now not allowing special characters like çöäüèéà or punctuation characters like ' or -):

/^([A-Za-z]+?)\s([A-Za-z]+?)$/

The length of the full string cannot be measured in this form, but can be easily read out with JavaScript:

name.length;

Here is an example in JavaScript:

var name = "Test Name";
if(name.match(/^([A-Za-z]+?)\s([A-Za-z]+?)$/) && name.length <= 30) {
    console.log("name is valid.");
}

Like @gaetanoM pointet out in the comments, you could also use a more sophisticated regular expression like it is described in this answer.

Community
  • 1
  • 1
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87