Thread: Validation of user input

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    27

    Validation of user input

    Hello

    I would like to know how to validate user input in terms of:

    Length of input
    Input format (I would like to restrict to hex)

    and any other useful links regarding the matter

    thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If i were you i would create my own function(which maybe would use manufactured functions) in order to validate input.For exampe for length of input,in case we have a string,i would pass the string as an argument at a function,which would check the length with strlen and then my function would return true or false.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Ok ill look at that:

    I'm basically gonna say

    Please insert start address (in hex):

    Please insert end address (in hex):

    If it isn't hex or valid (within the variable hex) then display error


    I think I can figure it out once I know how to check if data inputted is hex - how would I do this?
    Last edited by mrapoc; 08-04-2012 at 08:17 AM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You could check if the string contains what a hex should contain (A-F, 0-9).InStr() is useful to see if a specific character exists in a string.
    (these i wrote to you after googling )

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by mrapoc View Post
    Ok ill look at that:

    I'm basically gonna say

    Please insert start address (in hex):

    Please insert end address (in hex):

    If it isn't hex or valid (within the variable hex) then display error
    Is there a specific reason you only want to accept hexadecimal numbers? In the end it should be the value that counts not what base is used, right. I would consider fgets() to get a string to a buffer, then strtol to get the number from the string. strtol accepts hexadecimal, octal and decimal numbers and can also detect if the input is not a number. Once you have your values you can also check if the range makes sense, if that is necessary.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Well I suppose as long as when its converted to hex it makes sense...

    In that case...I only need one check and thats to ensure that the value is within the range

    The user could input 255, which is FF - I see what you mean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User input validation error
    By xeon321 in forum C Programming
    Replies: 6
    Last Post: 06-28-2012, 06:20 AM
  2. Input Validation
    By caprice150 in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2011, 06:18 AM
  3. Beginner - User Validation & encryption
    By darksifer in forum C Programming
    Replies: 4
    Last Post: 11-22-2010, 10:07 AM
  4. Input/Validation
    By nokia123456 in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 11:26 AM
  5. Input Validation!
    By noaksey in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 05:19 AM