MFC,如何检查CString格式是否与IP格式匹配,
例如用户输入
192.168,1,1错误格式
256.256.2.2错误格式
192.168.2错误格式
有些提示告诉我,thx
答案 0 :(得分:0)
使用Find()
,Mid()
或Tokenize()
搜索字符串,并根据一些必要规则验证其内容:
e.g。
答案 1 :(得分:0)
好的,我找到了解决方案
// 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;
}