validates characters

L

Lyle Ladeira

Hey all

i would like to know if someone can please help me.
i would like to know how to validate characters so that the user would not
be able to enter numbers into a characters field.

thanx
 
J

John Harrison

Hey all

i would like to know if someone can please help me.
i would like to know how to validate characters so that the user would
not
be able to enter numbers into a characters field.

thanx

Well first you have to define what you want. What do you mean by number?

123 - this is a number, so it isn't valid, right?

12ab - is this valid or not? its not a number but it does have numbers in
it

12.3 - what about this?

-123 - what about this?

So I think you need to specify exactly what input is valid and what is
not, then someone will be able to help.

John
 
L

Lyle Ladeira

hey

the only thing that should be valid is letters and spaces
A-Z a-z and spaces, tabes etc...

thanx
 
O

osmium

Lyle said:
i would like to know if someone can please help me.
i would like to know how to validate characters so that the user would not
be able to enter numbers into a characters field.

The way that is usually recommended is to get the entire line, perhaps using
getline(), see if a suitable number is in the line, and then do your own
conversion. The easiest way is probably to use <cstdlib> functions such as
strtod() and strtol(); although you can do this with "pure" C++ techniques
too. If you find a nice number, go on. Otherwise ask the user to reenter
the entire line.

A second way is to read as though you expect the user to do a proper entry
and then clear up the mess created in the stream if he doesn't. You must
clear the fail state and empty the buffer, in *that order*. see
ios::clear() and istream::ignore(). Then ask the user to try again.

The first way is probably easier and you may learn more of long term
interest that way, too.
 
J

John Harrison

Lyle Ladeira said:
hey

the only thing that should be valid is letters and spaces
A-Z a-z and spaces, tabes etc...

thanx

OK, the way to do this is to loop through all the characters of the string
testing each one. As soon as you find an invalid character then you know
that the whole string is invalid. If you don't find an invalid character
then the string is valid. Something like this

#include <ctype.h>

bool is_valid(const char* str)
{
while (*str)
{
if (!isalpha(*str) && !isspace(*str)) // if not alphabetic and not a
space ...
return false; // .. then its not valid
++str;
}
return true; // all chars tested ok so its valid
}

john
 
I

Ioannis Vranos

Lyle said:
Hey all

i would like to know if someone can please help me.
i would like to know how to validate characters so that the user would not
be able to enter numbers into a characters field.



You may use <cctype> facilities, like isalpha(), isdigit() etc.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top