I need to Validate the string . The string has format like "V02:01:02:03:04".
What I am trying to do :
1) I want to check correct length is provided
2) First character is 'V'
3) All other characters are 2 letter digits and separated by colon and at correct position.
4) There is no blank space tabs et.c
4) Cant think of any more validation .
What I have done so far
1) Function has easily check len and fist char not verify that subsequent chars after first does not contain alpha or space. But I am bit stuck about how to check colon positions and 2 letter char after every colon.
Below is my attempt.
int firmwareVerLen = 15;
const std::string input = "V02:01:02:03:04"; // this is the format string user will input with changes only in digits.
bool validateInput( const std::string& input )
{
typedef std::string::size_type stringSize;
bool result = true ;
if( firmwareVerLen != (int)input.size() )
{
std::cout <<" len failed" << std::endl;
result = false ;
}
if( true == result )
{
if( input.find_first_of('V', 0 ) )
{
std::cout <<" The First Character is not 'V' " << std::endl;
result = false ;
}
}
if( true == result )
{
for( stringSize i = 1 ; i!= input.size(); ++ i )
{
if( isspace( input[i] ) )
{
result = false;
break;
}
if( isalpha(input[i] ) )
{
cout<<" alpha found ";
result = false;
break;
}
//how to check further that characters are digits and are correctly separated by colon
}
}
return result;
}
If it's such a strict validation, why not check character by character? for example (given you've done size already)
if (input[0] != 'V')
return false;
if (!std::isdigit(input[1]) || !std::isdigit(input[2]))
return false;
if (input[3] != ':')
return false;
// etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With