最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for Name validation with atleast one alpahbet(A-z) - Stack Overflow

programmeradmin1浏览0评论

I need a regex that should validate it should contain atleast one alphabet(A-Z,a-z) and optionally numbers(0-9).

Valid names:

  • aaaa
  • aaaa1
  • SO
  • John P. Example

Invalid names,

  • 1111
  • @@@@

the Regex I have tried so far

[a-zA-Z0-9\.\'\-_\s]{1,20}  

and

function validateFirstName(a) {
  if (/[0-9]/.test(a) && /[a-zA-Z]/.test(a)) {
      return false;
  } else {
      return true;
  }
}

but both doesn't work.

Can anyone help in this regard?

I need a regex that should validate it should contain atleast one alphabet(A-Z,a-z) and optionally numbers(0-9).

Valid names:

  • aaaa
  • aaaa1
  • SO
  • John P. Example

Invalid names,

  • 1111
  • @@@@

the Regex I have tried so far

[a-zA-Z0-9\.\'\-_\s]{1,20}  

and

function validateFirstName(a) {
  if (/[0-9]/.test(a) && /[a-zA-Z]/.test(a)) {
      return false;
  } else {
      return true;
  }
}

but both doesn't work.

Can anyone help in this regard?

Share Improve this question edited Dec 29, 2015 at 6:14 Lucky asked Feb 8, 2013 at 13:16 LuckyLucky 17.4k19 gold badges120 silver badges156 bronze badges 3
  • "Doesn't work" means what exactly? It matches not everything it should match? It matches even what it should not match? You get a syntax error? You get a runtime error? – Felix Kling Commented Feb 8, 2013 at 13:18
  • Well, /[0-9]/.test('aaaa') obviously results in false. – Felix Kling Commented Feb 8, 2013 at 13:20
  • Please don't consider that you can validate firstname like this. Names are a really plicated a weird world, your requirement would simply cut millions of people from entering their own name. – Clement Herreman Commented Feb 8, 2013 at 13:20
Add a ment  | 

5 Answers 5

Reset to default 5

Try:

^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

To explain:

  • The bit in the middle square brackets (the "meat in the sandwich") matches an alphabet character. The + after it makes sure there is at least one of these.
  • The other two square-bracketed expressions (the "bread in the sandwich") match alphanumeric characters. The * after each allow any number of these.
  • The ^ and $ surrounding the whole thing make sure it is the whole text being looked at and not just part of it.

[a-zA-Z]+.*|.*[a-zA-Z]+|.*[a-zA-Z]+.* match the examples you supplied.

So you want any number of a-z char or number, then at least one a-z char and any number of a-z char or number again:

^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

This should work fine.

If you only want t check whether a string contains an alphabetic character, then simply do:

/[a-z]/i.test(str)

If the string should be posed of only alphanumeric characters with at least one alphabetic character:

/^(?=.*[a-z])[a-z\d]+$/i.test(str)

or

/[a-z]/i.test(str) && /^[a-z\d]+$/i.test(str)

Otherwise, [a-zA-Z0-9.'\-_\s]{1,20} looks good to me actually, but you have to anchor it to the beginning and end of the string:

/^[a-zA-Z0-9.'\-_\s]{1,20}$/.test(str)

If you want to enforce an alphabetic character, you have to include the lookahead or make an extra test, just like in the previous example.

I went to the Angular Docs and found this. Maybe it is very simple but does what I need

<input name="firstName" ngModel pattern="[a-zA-Z ]*">

https://angular.io/api/forms/PatternValidator#adding-a-pattern-validator

发布评论

评论列表(0)

  1. 暂无评论