?
Solved

how can i validate that a string only contains digits

Posted on 2003-03-01
4
Medium Priority
?
292 Views
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.
0
Comment
Question by:daveyj1572
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Gula
ID: 8048424
are you using C?
0
 
LVL 1

Expert Comment

by:Gula
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'
0
 
LVL 1

Accepted Solution

by:
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 */

0
 
LVL 5

Expert Comment

by:Kocil
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 :

TForm1::OnClose()
{
   return check(Edit1->Text, 6);
}

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


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

752 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