rahulme81 has asked for the wisdom of the Perl Monks concerning the following question:

Hi What could be the regular expression to check if the textbox contains only special characters only.

text pattern which I want to allow in text box can be "ABC" or comma separated list as "ABC,DEF,GHI_JKL". Don't want user to enter * or *,* or *.* to be entered in test box

Thanks

  • Comment on regular expression to check if the textbox contains only special characters

Replies are listed 'Best First'.
Re: regular expression to check if the textbox contains only special characters
by toolic (Bishop) on Oct 17, 2016 at 13:09 UTC
    Just check for at least one upper-case letter:
    use warnings; use strict; while (<DATA>) { if (/[A-Z]/) { print 'good '; } else { print 'bad '; } print; } __DATA__ * *,* *.* ABC ABC,DEF,GHI_JKL

    Outputs:

    bad * bad *,* bad *.* good ABC good ABC,DEF,GHI_JKL
      I'm not sure whether the OP wants *.*,*;A to pass. I'd use
      chomp; print /[^A-Z_,]/ ? 'bad' : 'good'

      Or, if the format is important, too:

      print /^(?:[A-Z_]+,?)+(?<!,)$/ ? 'good' : 'bad';

      i.e. at least one uppercase letter or underscore, followed by a comma, this repeating several times, and no trailing comma.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
         print /^(?:[A-Z_]+,?)+(?<!,)$/ ? 'good' : 'bad';

        Hi I tried your suggestion as below

        while (<DATA>) { if (/^(?:[A-Z_]+,?)+(?<!,)$/) {print "good";} else {print "bad";} print; }
        __DATA__ * *,* *.* ABC ABC* *ABC ABC_TO ABC_TO,DEF,GHI_JKL,GHI_JK1_TO

        Good string format are ABC,ABC_TO and ABC_TO,DEF,GHI_JKL,GHI_JK1_TO but somehow I am getting this as BAD ABC_TO,DEF,GHI_JKL,GHI_JK1_TO

Re: regular expression to check if the textbox contains only special characters
by ww (Archbishop) on Oct 17, 2016 at 14:08 UTC

    Strongly suggest you visit (and study) the Tutorials section devoted to regexen. You wouldn't have needed to post your question had you made an effort to learn. Downvoted.

    Yet another gimmé request!
    Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.

Re: regular expression to check if the textbox contains only special characters
by RonW (Parson) on Oct 17, 2016 at 17:51 UTC
    I want to allow in text box can be "ABC" or comma separated list as "ABC,DEF,GHI_JKL". Don't want user to enter * or *,* or *.* to be entered in test box

    Seems like your acceptable characters are A through Z and _ and ,

    So, I would test for anything matches the inverse of that character set:

    if ([^A-Z_,]) { # bad user input } else { # good user input }

    Disclaimer: Not tested. YMMV

Re: regular expression to check if the textbox contains only special characters
by Linicks (Scribe) on Oct 17, 2016 at 18:38 UTC

    You can use bog standard HTML to do this - all 'modern' browsers support it:


    <input type="text" ... ... maxlength="15" pattern="[a-zA-Z]{1,15}"

    will only allow UPPER/lower case a to z up to a maximum length of 15 characters.

    Nick

      This is interesting and worth mentioning but needs a clear caveat that the backend has no idea whether the data came from an HTML5 browser or a hacker/hackbot that bypassed the filter completely. Client side validation is for courtesy, speed, saving unnecessary server load. All data must be validated on the backend/server regardless of what the front end does.

      I also think it borders or a poor practice because the best code would use the same regex on the front and back end to avoid silly bugs and the backend has much more power to offer so will be hobbled by the subset the front end can do.

        print /^(?:[A-Z_0-9]+,?)+(?<!,)$/ ? 'good' : 'bad';

      Thankyou everyone.