in reply to regular expression to check if the textbox contains only special characters

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

  • Comment on Re: regular expression to check if the textbox contains only special characters
  • Download Code

Replies are listed 'Best First'.
Re^2: regular expression to check if the textbox contains only special characters
by Your Mother (Archbishop) on Oct 18, 2016 at 12:20 UTC

    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.

Re^2: regular expression to check if the textbox contains only special characters
by rahulme81 (Sexton) on Oct 18, 2016 at 10:20 UTC
      print /^(?:[A-Z_0-9]+,?)+(?<!,)$/ ? 'good' : 'bad';

    Thankyou everyone.