Frage

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

War es hilfreich?

Lösung 2

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;
    }

Andere Tipps

Search the string, using Find(), Mid() or Tokenize() and validate it's contents, based on a few required rules:

e.g.

  1. no alpha characters
  2. precisely 3 periods (.)
  3. numbers between periods
  4. 0 <= number <= 255
  5. first number > 0
  6. etc...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top