0

How can I validate a name in Javascript using Regex? I tried with /^([a-zA-Z.]){3,50}$/ this validation expression but it does not completely satisfy my needs.

Valid Names can be (Total Length between 3 and 50)

  • John
  • Antony Simon
  • Kevin M D
  • James.K

Invalid names can be

  • 121212
  • aaa12212
  • 3434fgfgf
  • James..
  • James .

Please help.

1

3 Answers 3

2

First try to decide what will be your validation criteria one by one.....
1.Is it just to check string length Length between 3 and 50
2.Is it just to check string length Length between 3 and 50 and just contain characters
3.Is it just to check string length Length between 3 and 50 and just contain characters '.' sysmbol etc
Without deciding your proper scenario you can't build a regex

Sign up to request clarification or add additional context in comments.

Comments

1

Try this one:

/^([a-zA-Z. ]){2,49}[a-zA-Z]$/

1 Comment

+1, but you don't need capturing parentheses.
0

Try this it should work ..

var str = "James..";
var regx =  /^(([a-zA-Z](\s|\.)?)*){3,50}$/;
regx.test(str);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.