4

I want to make a JavaScript regular expression that checks for valid names.

  • minimum 2 chars (space can't count)
  • space en some special chars allowed (éàëä...)

I know how to write some seperatly but not combined.

If I use /^([A-Za-z éàë]{2,40})$/, the user could input 2 spaces as a name

If I use /^([A-Za-z]{2,40}[ éàë]{0,40})$/, the user must use 2 letters first and after using space or special char, can't use letters again.

Searched around a bit, but hard to formulate search string for my problem. Any ideas?

3
  • 2
    Sooo, what happens to people that have chars that you don't allow in their name?? Commented Feb 25, 2012 at 15:34
  • 1
    @loganfsmyth they get a new name ;) Commented Feb 25, 2012 at 15:38
  • 2
    kalzumeus.com/2010/06/17/… Commented Oct 21, 2013 at 8:11

6 Answers 6

14

Please, please pretty please, don't do this. You will only end up upsetting people by telling them their name is not valid. Several examples of surnames that would be rejected by your scheme: O'Neill, Sørensen, Юдович, . Trying to cover all these cases and more is doomed to failure.

Just do something like this:

  • strip leading and trailing blanks
  • collapse consecutive blanks into one space
  • check if the result is not empty

In JavaScript, that would look like:

name = name.replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s+/, " ");
if (name == "") {
  // show error
} else {
  // valid: maybe put trimmed name back into form
}
Sign up to request clarification or add additional context in comments.

Comments

4

Most solutions don't consider the many different names there might be. There can be names with only two character like Al or Bo or someone that writes his name like F. Middlename Lastname.

This RegExp will validate most names but you can optimize it to whatever you want:

/^[a-z\u00C0-\u02AB'´`]+\.?\s([a-z\u00C0-\u02AB'´`]+\.?\s?)+$/i

This will allow:

  • Li Huang Wu
  • Cevahir Özgür
  • Yiğit Aydın
  • Finlay Þunor Boivin
  • Josué Mikko Norris
  • Tatiana Zlata Zdravkov
  • Ariadna Eliisabet O'Taidhg
  • sergej lisette rijnders
  • BRIANA NORMINA HAUPT
  • BihOtZ AmON PavLOv
  • Eoghan Murdo Stanek
  • Filimena J. Van Der Veen
  • D. Blair Wallace

But will not allow:

If the name needs to be capitalized, uppercase, lowercase, trimmed or single spaced, that's a task a formatter should do, not the user.

2 Comments

I believe this is the best choice for those who want to allow only latin alphabetic characters. I only added dash to Santiago's regex: /^[a-z\u00C0-\u02AB'´`-]+\.?\s([a-z\u00C0-\u02AB'´`-]+\.?\s?)+$/i. In addition, it's possible to use the same approach regarding unicode characters to add other possibilities. By the way, I used it in javascript.
Updated version (a bit more flexible): gist.github.com/stgogm/32b54c4a5d00e73dedb2
2

I would like to propose a RegEx that would match all latin based languages with their special characters:

/\A([ áàíóúéëöüñÄĞİŞȘØøğışÐÝÞðýþA-Za-z-']*)\z/

P.S. I've included all characters I could find, but please feel free to edit the answer in case I've missed any.

Comments

1

Why not

var reg= /^([A-Za-z]{2}[ éàëA-Za-z]*)$/;

2 letters, then as many spaces, letters or special characters as you want.

I wouldn't allow spaces in usernames though - it's begging for trouble when you have usernames like

ab         ba 

who's going to remember how many spaces they used?

1 Comment

its actually to fill in a form, and used to check for lastname. Since there are many last names with spaces in them i wanted to include them. But as you point out it is possible with your script to use many spaces in a row. Is there an easy solution to allow spaces but only one at a time, so you can have a lastname like: van den borre (and still refuse names like van den borre) ? Thx for the answers allready :)
1

You could do this:

/^([A-Za-zéàë]{2,40} ?)+$/

2-40 characters, and then optionally a space, repeated at least once. This will allow a space at the end, but you could trim it off separately.

3 Comments

If you want to get rid of the space in the regex itself, use a non-capturing group. /^(?:([A-Za-zéàë]{2,40}) ?)+$/
@loganfsmyth I mean that applying the regex (with, say, test) to a string like "bob barker " would result in true, where you might want it to be false because of the space on the end.
Oh yeah, I must have misunderstood :P Oops
0

After 'trim' the input value, The following will math your request only for Latin surnames.

rn = new RegExp("([\w\u00C0-\u02AB']+ ?)+","gi");
m = ln.match(rn);
valid = (m && m.length)? true: false;

Note that I am using '+', instead of '{2,}', that is because some surnames uses just one letter in a separated word like "Ortega y Gasset"

You can see I am not using RegExp.test, this is because that method don't work properly (I don't know why, but it has a high fail-rate, you may see it here:.

In my country, people from non-latin-language countries usually do some translation of their names so the previous RegExp would be enough. However, if you attempt to match any surname in the world, you may add more range of \u#### characters, avoiding to include symbols, numbers or other type. Or perhaps the xregexp library may help you.

And, please, do not forget to test the input in server side, and escaping it before using it in the sql sentences (if you have them)

Comments

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.