• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: Name and Surname validation using regular expressions (JavaScript)

Resolved: Name and Surname validation using regular expressions (JavaScript)

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Using regular expressions check that the name and surname contain only uppercase and lowercase letters of Latin and Cyrillic, spaces and dashes (minus sign).
The first character must be a capital letter of Latin or Cyrillic, the other characters must be lowercase letters of Cyrillic or Latin; after the second character, a space or dash is allowed; after a space / dash the word must start with a capital letter again, and the other letters must be lowercase; again after the second character there may be a new space / dash, but at the end it must end with a word beginning with a capital letter; it is not allowed to stand two spaces or dashes next to each other, nor a space and a dash, it must contain at least one space / dash. It must not be shorter than 5 characters or longer than 180.
In case the name and surname are not in the correct format, show an orange box around the field and below the field, in orange letters show the error “Name and surname not written correctly”. Show the error after leaving the field for entering the name and surname.
If the user corrects the name and surname, after leaving the field again, remove the orange box and the error below.
Names available:

Петар Петровић Petar Petrović Petar Petrovic Petar Petrović-Njegoš Jovana-Stefanija Ivanović-Vasić Petrović


Invalid names and surnames:

Ud A Уд Aд Уд_Ад Ud. Ad Петар 2 Петровић Petar P Petrović Jovan J. Jovanović P2etar Grbić Petar Petrović-njegoš Petar petrović




How can I limit the number of characters and add Cyrillic? Also Fname and Lname is required. Thanks!

Answer:

As the strings contain Serbo-Croatian characters too, I check them as well.

Regex Explanation


  • (?=^.{5,180}$) Asserts that the string length be at least 5 and at most 180
  • ^[А-Я][а-яčćžđšžћ]+(?:[\s-][А-Я][а-яčćžđšžћ]+)+$ Matches names in latin (and Serbo-Croatian) letters
  • ^[A-Z][a-zčćžđšžћ]+(?:[\s-][A-Z][a-zčćžđšžћ]+)+$ Matches names in Cyrillic (including Serbo-Croatian) letters

Also, see the regex demo

If you have better answer, please add a comment about this, thank you!

javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to efficient create SimpleITK image?

01/04/2023

Resolved: How can I write CSS selector(s) that apply to table rows for all td elements on that row after a td with a certain class?

01/04/2023

Resolved: How do I use SetWindowText with Unicode in Win32 using PowerShell?

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.