MFC, How do I check the CString format is match the IP Format,
For example User input
192.168,1,1 Error format
256.256.2.2 Error format
192.168.2 Error format
Some tip tell for me, thx
MFC, How do I check the CString format is match the IP Format,
For example User input
192.168,1,1 Error format
256.256.2.2 Error format
192.168.2 Error format
Some tip tell for me, thx
Search the string, using Find()
, Mid()
or Tokenize()
and validate it's contents, based on a few required rules:
e.g.
OK, I find the solution
// precisely 3 periods (.), my ip is save if strCtrlIP
CString strCheck(strCtrlIP);
int nPointNum = 0;
nPointNum = strCheck.Remove('.');
if(nPointNum != 3)
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}
// numbers between periods, 0 <= number <= 255
strCheck.Format(_T("%s"), strCtrlIP);
while(strCheck.Find(_T(".")) >= 0)
{
int nLoc = strCheck.Find(_T("."));
int nVal = _ttoi(strCheck.Left(nLoc));
strCheck = strCheck.Right(strCheck.GetLength() - (nLoc+1)); // egnore point
if(nVal < nUserLimitDown || nVal > nUserLimitUp || strCheck.IsEmpty())
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}
}
if(_ttoi(strCheck) < nUserLimitDown || _ttoi(strCheck) > nUserLimitUp)
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}