Thread: Best method of string validation?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17

    Best method of string validation?

    In your opinion, what is the best method for validating a user input string.
    For example , to make sure that the user input 7 digits as opposed to letters or a mix of the two.

    Should I use a loop to check isdigit() for each character, or is there a way to look at the string as a whole?

    What would this kind of structure look like?

    Regards,
    Braydon

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by daynorb
    to make sure that the user input 7 digits (...) Should I use a loop to check isdigit() for each character
    That sounds like a good solution, assuming that you also check that exactly 7 digits were entered. Is it the best? Maybe, maybe not (e.g., if a regular expression library were available, maybe using that would be better, or it might be overkill).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validation of string format
    By Evenstevens in forum C Programming
    Replies: 8
    Last Post: 05-03-2009, 05:04 AM
  2. string validation
    By sql.scripter in forum C++ Programming
    Replies: 31
    Last Post: 05-24-2005, 03:11 PM
  3. String Validation
    By duvernais28 in forum C Programming
    Replies: 15
    Last Post: 01-30-2005, 08:11 PM
  4. validation of a string/check if exists
    By colinuk in forum C Programming
    Replies: 19
    Last Post: 01-28-2005, 08:19 PM
  5. String Validation (Newbie)
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 01-26-2005, 08:22 AM