-2

I am not able to this regex as alphanumeric.This regex is fulfilling all my requirements but it is not alphanumeric i.e it must validate if the value entered is alpha numeric or not.The regex is below

^
  (?!(.)\1+$)
  (?!.*([a-zA-Zа-яА-Я0-9ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ0-9]\d)\2{3})
  (?!.*([ '"-])\3)(?![a-zA-Zа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ0-9\d]*\W{2})
  (?=.*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿè-ÿ0-9\d])
  (?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*)
  [a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿè-ÿ0-9\d\ *"(:,\/.\\;&)\[\]_+#'-]{3,40}
$

It should accept all the below criteria.It is fullfilling all the criteria only "It Must have at least 3 compulsory alpha-numeric characters excluding special character" is not being fullfilled. The regex should fullfill the below conditions:

  • Minimum of 3 characters (includes alphabets and numbers, not including Special Characters )
  • It Must have at least 3 compulsory alpha-numeric characters excluding special characters
  • All alphabets cannot be the same in a standalone word. However, if they are a part of the word, we will allow the same. For example - AAA is not allowed but Haaadoop is allowed
  • Consecutive alphabets less than or equal to 3 are allowed in a word. Ex– Haaadoop is allowed but Haaaadoop is not allowed.
  • Three or more consecutive Standalone alphabets are not allowed. Example - AA is allowed. AAA is not allowed.
  • All numbers can be the same. Example – 111 is allowed
  • If more than 1 special character (In a word or Standalone) are consecutively entered, it is invalid. Example A-B is allowed, A- -B or A-&B is not allowed
  • Allow special characters #&()_+[]:;',/.-"*
  • Consecutive dashes, apostrophes are not allowed in any part of the string
  • It should accept these international and cyrilic characters as well ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçАаБбВвГгДдЕеЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯяèéêëìíîïðñòóôõöøùúûüýþÿ
  • These international and cyrilic characters should be accepted in starting of the word or in between of the words or in the end of the word

The acceptance criteria is:

Haaadoop 123
A-B 123
123 âãäåæçèéêëìíî
нОоСсТт 123
123 нОоСсТт
нОр123Тт
123 Haaadoop
123 A-B
âãäåæçèéêëìíî 123
âãäåæ123çèéêëìíî

It should not accept:

Haaaaadoop 123
A- -B 123
A- -B 123
A-&B123
AAA
abcd
123
123 Haaaaadoop
123 A- -B
123 A- -B
2 a
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • *it must validate if the value entered is alpha numeric or not* - so, you do not care if the value is `122ddgg` or `#$#$^%`? – Wiktor Stribiżew Oct 19 '16 at 07:41
  • 1
    @WiktorStribiżew It seems the regex is much more than that, at least I see something like `Post Office` in it – kennytm Oct 19 '16 at 07:43
  • 1
    @kennytm: Yesterday I spent some half an hour on this regex and I see OP did not get it right (I see duplicated patterns), but I can't understand the requirements. – Wiktor Stribiżew Oct 19 '16 at 07:44
  • Wik its accepting all alpbhates or numbers or both.But what i am trying to make it it shud be only alphanumeric as 123 abc should be accepted.it should not accept 123 or abc.It should accept 123 abc only. – Pratttsssssssssssss Oct 19 '16 at 07:45
  • 1
    @PratyakshChaudhary: Please edit the question, and list some examples what must be accepted by the regex, and what must be rejected by the regex. – kennytm Oct 19 '16 at 07:46
  • @wiktor shall i share the acceptance critera.As because i have shared the requirements in the comment box in my previous post and made some editings as well – Pratttsssssssssssss Oct 19 '16 at 07:46
  • 2
    Well, try-game again. Add `(?!(?:\d+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-Яè-ÿ]+)$)` after `^`. See https://regex101.com/r/iZsyTL/1 – Wiktor Stribiżew Oct 19 '16 at 07:49
  • it is working.But other validations are not working – Pratttsssssssssssss Oct 19 '16 at 09:11
  • Sorry, your requirements contradict one another. *Three or more consecutive Standalone alphabets are not allowed. Example - AA is allowed. AAA is not allowed.* - HOW? there min length is 3 and only alpha or digit only input is not accepted. Also, *All numbers can be the same. Example – 111 is allowed* - but input cannot be digit only. – Wiktor Stribiżew Oct 19 '16 at 10:31
  • @WiktorStribiżew we need to focus on AAA because AA wont be accepted because the minimum length will be 3.For 111 is allowed and the input must be the combination of alphabets and digits.All the requirements are working in the regex which i have mentioned in the question,only the problem is that it must be alphanumeric.this validation is for the address field – Pratttsssssssssssss Oct 19 '16 at 10:54
  • By three or more standalone characters means alphabets not numbers – Pratttsssssssssssss Oct 19 '16 at 10:56
  • @WiktorStribiżew minimum length is 3 and it should be alphanumeric.But 3 consecutive same alphabets are not allowed.Ex: 111 Abc is allowed but 111 AAA is not allowed. – Pratttsssssssssssss Oct 19 '16 at 11:00
  • Again, if your regex is working, and you need to fail any string that is all digits OR all "alphabets" (=*letters*), you just need to add `(?!(?:\d+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-Яè-ÿ]+)$)` after `^`. *Everything* should work as it worked before adding that lookahead as it contains no capturing groups. – Wiktor Stribiżew Oct 19 '16 at 12:29
  • @WiktorStribiżew we r trying below regex.But нОр123Тт or нОоСсТт 123 are not being acceptedf(?!(.)\1+$)(?!.*([a-zA-Zа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ])\2{3})(?!.*([ '"-])\3)(?![a-zA-Zа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ]*\W{2})(?=.*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿè-ÿ])(?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*)[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿè-ÿ0-9\ *"(:,\/.\\;&)\[\]_+#'-]{3,40}$ – Pratttsssssssssssss Oct 19 '16 at 13:41
  • Do not spell out `ÁÂÃ‌​ÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØ‌​ÙÚÛÜÝÞߟàáâãäåæçèéêë‌​ìíîïðñòóôõöøùúûüýþÿ`, use one class for the letters - `[a-zA-ZÀ-ÖØ-ߟà-çа-яА-Яè-ÿ]`. Again, sorry, I cannot make out anything. I provided too many hints, use them as building blocks. – Wiktor Stribiżew Oct 19 '16 at 13:48
  • @WiktorStribiżew i am trying all your hints but not able to fix it нОр123Тт or нОоСсТт 123 this one is not working please help – Pratttsssssssssssss Oct 19 '16 at 13:50
  • I removed one lookahead, see https://regex101.com/r/iZsyTL/2 – Wiktor Stribiżew Oct 19 '16 at 14:00
  • its not accepting нОоСсТт 123 – Pratttsssssssssssss Oct 19 '16 at 14:01
  • its not accepting нОр123Тт – Pratttsssssssssssss Oct 19 '16 at 14:01
  • the acceptance criteria is: Haaadoop 123 , A-B 123 , 123 âãäåæçèéêëìíî, нОоСсТт 123 , 123 нОоСсТт ,нОр123Тт, 123 Haaadoop , 123 A-B , âãäåæçèéêëìíî 123 , âãäåæ123çèéêëìíî It should not accept: Haaaaadoop 123, A- -B 123 , A- -B 123 , A-&B123 , AAA , abcd , 123 , 123 Haaaaadoop , 123 A- -B ,123 A- -B.Please help me @WiktorStribiżew i am struggling in this regex from past 15 days – Pratttsssssssssssss Oct 19 '16 at 14:10
  • Check https://regex101.com/r/iZsyTL/3 (the `\n` in the 4th and 5th lookaheads are only meant for the demo, they must be removed from the final pattern). – Wiktor Stribiżew Oct 20 '16 at 07:09
  • @WiktorStribiżew thanks alot its working fine with all the expected things.But is should not accept 2 characters i.e 2 a should not be accepted but it is being accepted.As because the requirement tells that it should accept atleast 3 complusory alphanumeric characters.But it is accepting .Its working fine for all the requiremnts but not fullfilling 2 a 2 ä 2 ж.As this should not be accepted – Pratttsssssssssssss Oct 20 '16 at 07:52
  • Ok, https://regex101.com/r/iZsyTL/4? – Wiktor Stribiżew Oct 20 '16 at 08:02
  • @WiktorStribiżew Thanks alot man.U r genius. – Pratttsssssssssssss Oct 20 '16 at 08:22
  • Why did you post another quesiton if - I guess - this one works? – Wiktor Stribiżew Oct 20 '16 at 08:37
  • This is working but that regex is different.@WiktorStribiżew.And if u see the requirements and acceptance criteria then both are different – Pratttsssssssssssss Oct 20 '16 at 08:40
  • Ok, I see. I found a redundant lookahead. Use [`^(?!(?:\d+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ]+)$)(?!(.)\1+$)(?!.*([a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9])\2{3})(?!.*[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]{2})(?=(?:[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]){3})(?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*)[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9 *"(:,\/.\\;&)\[\]_+#'-]{3,40}$`](https://regex101.com/r/iZsyTL/6). – Wiktor Stribiżew Oct 20 '16 at 09:09
  • @WiktorStribiżew it is showing pattern error.Please confirm – Pratttsssssssssssss Oct 20 '16 at 09:18
  • See the snippet below. – Wiktor Stribiżew Oct 20 '16 at 09:24
  • Hi,v ned to bld a regex with 1 more requirement.The acceptance criteria are Min of 2 alpha char,Numeric char r not allowed,All char can't be the same,Consecutive dashes, apostrophes are not allowed in any part of the string.The regex i m trying to implement is /(?!(.)\1+$)(?!.*( |'|-)\2{1})(?=([^ ].*[a-zA-ZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçАаБбВвГгДдЕеЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯяèéêëìíîïðñòóôõöøùúûüýþÿ]){1}.*$)^[a-zA-ZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞߟàáâãäåæçАаБбВвГгДдЕеЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯяèéêëìíîïðñòóôõöøùúûüýþÿ \.'-]{2,24}$/i, – Pratttsssssssssssss Oct 20 '16 at 09:38
  • @WiktorStribiżew all the conditons are working as expected.But it should alos not accept asdasd asdasdasdsadd , sdasd asdfsdafasdf , asdfasdf sdfdasfdsaf – Pratttsssssssssssss Oct 21 '16 at 13:45

1 Answers1

0

Well, here is the final regex:

^
  (?!(?:[\d ]+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ ]+)$)  # - no digit-only/alpha-only input (with spaces)
  (?!(.)\1+$)                                   # no same char input 
  (?!.*([a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9])\2{3}) # no same consecutive 4 alphanum chars
  (?!.*[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]{2})    # no 2 consecutive non-alphanum chars
  (?=(?:[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]){3}) # at least 3 alphanum chars in input
  (?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*) # No POST OFFICE BOX in input
   [a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9\ *"(:,\/.\\;&)\[\]_+#'-]{3,40} # 3 to 40 letters, digits and some special chars
$

A one liner:

/^(?!(?:[\d ]+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ ]+)$)(?!(.)\1+$)(?!.*([a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9])\2{3})(?!.*[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]{2})(?=(?:[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]){3})(?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*)[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9 *"(:,\/.\\;&)[\]_+#'-]{3,40}$/

See the regex demo

Note:

  • The letter ranges you need can be written with ranges, not spelled out (this shortens the pattern and makes it more readable): [a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ]
  • Note that Cyrillic ё and Ё do not fall into the а-я and А-Я ranges and must be added separately

var good = [ 'Haaadoop 123','A-B 123','123 âãäåæçèéêëìíî','нОоСсТт 123','123 нОоСсТт','нОр123Тт','123 Haaadoop','123 A-B','âãäåæçèéêëìíî 123','âãäåæ123çèéêëìíî'];
var bad = ['Haaaaadoop 123','A- -B 123','A- -B 123','A-&B123','AAA','abcd','123','123 Haaaaadoop','123 A- -B','123 A- -B','2 a', 'asdasd asdasdasdsadd', 'sdasd asdfsdafasdf', 'asdfasdf sdfdasfdsaf','132 123'];

var rx = /^(?!(?:[ \d]+|[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ ]+)$)(?!(.)\1+$)(?!.*([a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9])\2{3})(?!.*[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9\n]{2})(?=(?:[^a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]*[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9]){3})(?!.*[Pp]+(OST|ost)*\.*\s*[Oo0]*(FFICE|ffice)*\.*\s*[Bb]*\.*\s*[Oo0]*\.*\s*[Xx].*)[a-zA-ZÀ-ÖØ-ߟà-çа-яА-ЯЁёè-ÿ0-9\ *"(:,\/.\\;&)\[\]_+#'-]{3,40}$/;
document.body.innerHTML += "<br/>------ Valid ------<br/>";
for (var s = 0; s < good.length; s++) {                  // Demo
  document.body.innerHTML += "Testing \"<i>" + good[s] + "</i>\"... ";
  document.body.innerHTML += "Matched: <b>" + rx.test(good[s]) + "</b><br/>";
}
document.body.innerHTML += "<br/>------ Invalid ------<br/>";
for (var s = 0; s < bad.length; s++) {                  // Demo
  document.body.innerHTML += "Testing \"<i>" + bad[s] + "</i>\"... ";
  document.body.innerHTML += "Matched: <b>" + rx.test(bad[s]) + "</b><br/>";
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563