Using regular expression for input validation - Good practice or not?

Posted in QnA Arena on Apr 19, 2009 at 22:27 IST (about 1 year ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 10 of total 10 on page 1 of 1
Post reply
« Previous1Next »

lizard
Rank: 126

I am trying to develop some classes for general input validation, and I have being writing methods without any use of regular expressions. I recently read this Jeff's article. I thought that regular expressions were used to build front-ends for applications like parsers and anything else, but apparently they are used for much more than that.I realize that not all validations can or should be done with regular expressions, but are they a good practice to validate inputs?

Posted by lizard on Sunday, April 19, 2009, 10:27 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

viralbug
Rank: 19

I realize that not all validations can or should be done with regular expressions

You're exactly right on that one, but for things that need to be exactly a certain way (for example social security numbers, phone numbers (to some extent) and emails), using regular expressions can be helpful.

But do not rely on them. For example I have a pretty good email regular expression check but I also have a list of obviously bogus domains (example.com and some others i've seen in our database (mostly local stuff)) to match against.

Posted by viralbug on Sunday, April 19, 2009, 11:17 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

adrians
Rank: 223

Yes - regular expressions work very well for input validation. However, often times it's a very good idea to abstract these things away as much as possible as other methods - or even sometimes special validator objects.

Remember that regular expressions can often introduce a lot of trouble, but on the whole, input validation is a case where they fairly unconditionally shine.

Posted by adrians on Monday, April 20, 2009, 7:01 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

karbaas
Rank: 71

I don't know that I'd call it a best practice, but I certainly use regex for validation of things like email adresses and ops, among other things. If not a best practice, it's certainly a common practice.

Posted by karbaas on Monday, April 20, 2009, 8:21 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

mintea
Rank: 224

Regular expressions are just one way to match text against a pattern. There are other ways to do the same thing without using a regex. You shouldn't think of regular expressions as a buzzword that you must include in your code. Use whatever tool works the best.

For input validation just be sure whatever tools you're using let you specify exactly what kind of text you want to accept and reject everything else by default. Regular expressions let you do this easily and concisely for certain kinds of input, which is why many people use them.

Posted by mintea on Monday, April 20, 2009, 10:09 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

koistya
Rank: 320

Yes!

Regular expressions usually let you build a pretty solid input validation that's fairly readable in a very short space of time.

Something that does the right job, is maintainable and lets you get onto other things is good in my books.

As always, apply common sense and if a regex is a bad tool for the job, don't use it.

Posted by koistya on Monday, April 20, 2009, 11:34 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

marenda
Rank: 84

Using regexp validation is a good idea provided that you don't branch off into applying more than besic regular expressions:

If you find yourself validating potentially complex structures such as Michael Ash does in his attempt to verify a date you are off the beaten path and asking for trouble:

^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[13-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

Your code will suffer maintenance problems.

Posted by marenda on Tuesday, April 21, 2009, 10:27 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

bioblue
Rank: 319

The answer to your question really depends upon the purpose of your code. Yes, regular expressions are great and I agree, with all the previous answers (that I have read).

Using regular expressions, is a handy, quick and sleak way to validate certain inputs but perhaps what you need to remember what makes good code and apply the regular expressions in the correct places. I read the article you posted, and I thought the subject matter was about more about using regular expressions in the correct manner i.e. don't just use them for a solution because you know it will be quick to type up and it will work but in turn produces un-readable, lengthy and horrible looking code.

I wouldn't take it from what is written that regular expressions are "bad practice." I guess, he just wanted to put accross that sometimes you can spend a few more minutes considering design and come up with a better concept to implement or just conclude that regular expressions are that concept!

Posted by bioblue on Wednesday, April 22, 2009, 5:10 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

gopalwins
Rank: 219

You should validate on both the client and server sides. Regular expressions are very good for making sure that a string has a valid format (e.g, e-mail addresses, phone numbers, etc.), but the server should not depend solely on that. The server should check on its own and also validate business correctness (e.g., like the answer above that checked for bogus addresses in a database).

Once is not enough. There are different degrees of "valid".

Posted by gopalwins on Wednesday, April 22, 2009, 10:43 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

buttersc...
Rank: 317

If the input you are validating is in a regular language, then a regular expression is the right tool to validate it.

Posted by butterscotch on Thursday, April 23, 2009, 4:05 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.236 seconds