-1

I'm creating a web store and I need to validate inputs with JavaScripts so the user doesn't have to submit a form to be given the PHP errors (although I'm also validating the form with PHP).

What I came up with is the following regex:

/^[a-zA-Z]+$/

But the above regex would only allow alpha characters whereas I also want to allow characters such as ' and - since obviously names may also contain these two characters. My question is, how to make a regex to allow alpha characaters AND the two characters above.

Besides that I also have one more question which just came in my mind, characters such as ă will pass the above validation ?

Cristian D
  • 673
  • 5
  • 21
  • 8
    The best regex to validate names is `/^.*$/` Unless you're cool with hating Chinese, Japanese and Korean people, and any others whose names are written in non-Latin alphabets. – Niet the Dark Absol Jun 18 '14 at 14:00
  • I would advise against validating names. Why bother? – David Sherret Jun 18 '14 at 14:01
  • Do you really need to restrict their name entries to "correct" names? Let them worry about why they put a digit into their name, it won't break your app. – ajp15243 Jun 18 '14 at 14:01
  • 1
    http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – jgillich Jun 18 '14 at 14:02
  • Basically my point is avoiding $ or ! or numbers in names. – Cristian D Jun 18 '14 at 14:05
  • 1
    @ShowTime Why do you care about such characters? If it's just to have "nice looking name data", then you're adding unnecessary programming burden on yourself and interaction burden on your users. Ask yourself if you have a real business/application reason for avoiding "weird" characters. Also, you should read the article at jgillich's link. – ajp15243 Jun 18 '14 at 14:10
  • Indeed you are right, the article points lots of exceptions I'd have to take in account to validate names. I'll just allow any type of character in names and obviously transcript special characters such as `<` to their HTML equivalent in PHP so I just get rid of all the JavaScript hassle which can be easily bypassed anyway. – Cristian D Jun 18 '14 at 14:18
  • 2
    @ShowTime It's good that you recognize the difference between input validation and input sanitation :). – ajp15243 Jun 18 '14 at 14:21
  • Although I'm against any strict validation of names, in some scenarios it is vital to validate the names. For example for ID verification, the name user types into the form must match exactly to the names in the documents (e.g. drivers licence, passport, etc.). And you might be surprised how often people make typos or put special characters into the name fields. – Alexander Burakevych Sep 08 '17 at 01:15

1 Answers1

0

By adding them to your character group like so

/^[a-zA-Z'-]+$/
nozzleman
  • 9,529
  • 4
  • 37
  • 58
  • 2
    It should be noted that `-` should generally go at the very end or beginning of a `[ ]` character class if it is to be treated as a literal `-`, so as to avoid unintentional character ranges. – ajp15243 Jun 18 '14 at 14:02
  • 2
    I'll accept this as it answers the initial question, although I understood it's not necessary to validate names. – Cristian D Jun 18 '14 at 14:19