Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


how can i validate that a string only contains digits

Posted on 2003-03-01
Medium Priority
Last Modified: 2010-04-15
I need to check that a string only contains digits and is of a fixed length. The string that I want to check is on a separate dialog box. I want to validate the field before exiting so that I can force the user to enter in the correct format. I presume that the call will occur when clicking ‘OK' on that dialog box.
Question by:daveyj1572
  • 2

Expert Comment

ID: 8048424
are you using C?

Expert Comment

ID: 8048435
if it is C, you can try to use
int num = aoti(sume_string) function that converts the string to digit, and then check if it returned valid number, lenth of the string you can check with strlen(some_string)- returns number of characters in the string excluding '\0'

Accepted Solution

dceads earned 200 total points
ID: 8049077
You can use atoi(), but it will convert the string to a number even if it contains non-numeric digits.  So, you will need to check the numeric value to make sure that it is in range. For example, if you wanted to make sure that the value was 6 numeric digits you could use this

int num;
num = atoi(some_string);

if((num < 100000) || (num > 999999)) /* some_string is invalid */


Expert Comment

ID: 8062742
Check fixed length digit

int check(char *s, int len)
   while(len--) {
     if ((*s >= '9') || (*s <= '0')) return 0;
   return (*s ? 0 : 1);

Now to check it after the OK button is pressed and before the dialog is closed, it depends on the compiler/library.
For C++ Builder, use TForm and put that check on the
OnClose event, for example :

   return check(Edit1->Text, 6);

Hei ... is it C or C++ question ?


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question