1

Have a description box in which ajax is used to count number of characters . Need to validate the description box for input . The criteria is that it should allow everything - alphabets, spaces, new line characters, special characters etc. But , the input cant be only numbers/special characters/spaces alone !

Have been stuck on this for quite somewhile now , the regex that I have written leads to catastrophic backtracking error.

Please provide a solution for this .

shubbi
  • 21
  • 1
  • 4
  • So you mean there should be at least one occurance of each alphabets, spaces, new line characters, special characters ? – Ajay Narain Mathur Feb 07 '17 at 05:56
  • @A.J the only requirement is that there should be atleast one alphabet in the description box. Rest it can contain any thing else like numbers , whitespaces, special characters etc. – shubbi Feb 07 '17 at 07:55
  • @Wiktor , Currently Im using this ^.*[a-zA-Z]+.*$ – shubbi Feb 07 '17 at 07:56
  • It is working as expected for all validations but doesnot work when I press enter and type in the new line . Request you to please halp me with multiline regex as well. – shubbi Feb 07 '17 at 07:57
  • Replace `.` with `[\s\S]`. However, I can't see your code, it may not work for you as is. – Wiktor Stribiżew Feb 07 '17 at 07:58
  • @WiktorStribiżew, ^.*[a-zA-Z]+[\\s\\S] this is the expression Im using, It fails for new line character. After pressing enter , the regex fails. – shubbi Feb 07 '17 at 08:17
  • There is no point trying to change the regex, your issue is elsewhere. – Wiktor Stribiżew Feb 07 '17 at 08:18

1 Answers1

3
  • Things allowed: alphabets, special characters, spaces, new line and presumably numbers
  • Things not allowed: ONLY special characters, spaces,numbers and presumably new line
  • which leads to at least one alphabet present in the description

So this ^(.|\s)*[a-zA-Z]+(.|\s)*$ should help you

  • (.|\s)* --> any character including whitespace, if you want atleast one alphabet on first line then replace this with .*
  • [a-zA-Z]+ atleast one alphabet
  • (.|\s)* --> followed by any character including whitespace characters(like newline)
Abdul Hameed
  • 1,025
  • 12
  • 27
  • Hi, Thanks a lot . I tried . It works for everything . except when I press enter , the validation fails . Could you please tell how can I accomodate any number of enters. – shubbi Feb 07 '17 at 06:57
  • @shubbi edited the answer to include whitespace characters – Abdul Hameed Feb 07 '17 at 07:07
  • @shubbi if it works, mark the answer as correct , so that others will know – Abdul Hameed Feb 07 '17 at 07:15
  • @shubbi https://regex101.com/r/KZUmmJ/1 here you can test that regex.. it worked for me for multiline characters – Abdul Hameed Feb 07 '17 at 07:23
  • it doesn't work , You have mentioned how it will take in white space . But i'm looking for multiline regex which would allow me any number of new line characters as well . So i can type upto 2000 characters by giving spaces , new line(enter ) etc. – shubbi Feb 07 '17 at 07:43
  • `(.|\s)*` will cause unnecessary backtracking and might cause a slowdown, it is a very bad construct. Use `[\s\S]*` instead. – Wiktor Stribiżew Feb 07 '17 at 07:49
  • @WiktorStribiżew , I have tried [\s\S] as well. They dont work for new line characters. It allows only spaces . I want the regex to allow for multiple new line characters as well . – shubbi Feb 07 '17 at 08:05
  • Then your question is off-topic without code. `[\s\S]` matches *any* character. – Wiktor Stribiżew Feb 07 '17 at 08:06
  • thank you @WiktorStribiżew for the optimization.. do u have any links for the common optimizations in regex.? – Abdul Hameed Feb 07 '17 at 09:59
  • General idea is to avoid alternation when you do not need it, avoid nested quantifiers inside a pattern if there is one obligatory pattern and others are optional inside (or all are optional or only one is obligatory), avoid `(.|\s)*` / `(.|\n)*?` etc., use unroll the loop principle when dealing with long texts, avoid `.*` / `.*?` when parsing delimited texts... No idea where you can read about it. – Wiktor Stribiżew Feb 07 '17 at 10:08
  • @WiktorStribiżew, from ur comment in this question http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions i find rexegg.com to be a better tutorial.. thank u – Abdul Hameed Feb 07 '17 at 11:07
  • Thanks a lot all :) – shubbi Feb 07 '17 at 14:46