How to spellcheck, whether IP-Adress is correctly send

Hi friends,

I am currently programming an ESP8266 12E.

My aim is this:

The user have to type in its IP-Adress via HTML-INPUT Field.
My Software has to check, whether the User (Client) has typed in the Adress in a correctly form, like this 192.168.11.4 <-- so it must be all numeric, 3 dots must be their.

How can I do this ?

I have found a code, but this is not indeed, a numeric check.:

String str = "192.168.2.1"; // <-- this is an example to spellcheckString  
// String str = "h192.168.2.1"; // <-- this for example, could be a wrongfully spelled address


String strs[5];
int StringCount = 0;

void setup (void)
{
  Serial.begin (115200);
  delay(200);



  while (str.length() > 0)
  {
    int index = str.indexOf('.');
    if (index == -1) // No Dot found
    {
      strs[StringCount++] = str;
      break;
    }
    else
    {
      strs[StringCount++] = str.substring(0, index);
      str = str.substring(index+1);
    }
  }



  for (int i = 0; i < StringCount; i++)
  {

    long myInt = strs[i].toInt();

    // I have tried this for a spellcheck
    // but if you put for example a small 'h', than a Zero comes out of the integer
    // But Zeros are also used as an IP Adress
    if(myInt < 1000) 
    {
    Serial.println(myInt);
    }
  }
}

void loop () {}

Perhaps this could help.

1 Like

wait will try

i can not test it:

allways this error apear:

invalid conversion from 'char*' to 'int' [-fpermissive]

char * myChar = "65465";


void setup() {

}

void loop() {
  if (isDigit(myChar)) {  // tests if myChar is a digit
    Serial.println("The character is a number");
  }
  else {
    Serial.println("The character is not a number");
  }

}

This has probably been done a hundred times before, but here goes.

bool isByte(int const number) {
  return number >= 0 and number < 256;
}

bool isValid(char const* text) {
  return isDigit(text[0]) and text[0] != '0' and isByte(atoi(text));
}

bool isIP(char const* text) {
  size_t startPos = 0;
  size_t pos = 0;
  size_t dots = 0;

  while (text[pos]) {
    if (not isDigit(text[pos])) {
      if (not (text[pos] == '.' and isValid(&text[startPos]))) {
        return false;
      }
      startPos = pos + 1;
      dots++;
    }
    pos++;
  }

  return isValid(&text[startPos]) and dots == 3;
}
1 Like

thanks, but where I have to put the IP-Adress to check ?

plese note: I am a beginner.

And where is the setup() and loop() ?

I have just ran the code you given, and and Error occures.

The rest of the sketch could look as follows.

void setup() {
  Serial.begin(9600);

  Serial.println(isIP("192.168.2.1"));
  Serial.println(isIP("192.168.2"));
  Serial.println(isIP("192.168.2.1.3"));
  Serial.println(isIP("192.168.2.333"));
  Serial.println(isIP("292.168.2.1"));
  Serial.println(isIP("f92.168.2.1"));
  Serial.println(isIP("19f.168.2.1"));
}

void loop() {}

This will print 1 for the first string and 0 for the rest of the strings.

1 Like

Thanks it works :smiley:

But:

But why are these both not Ip-Adresses ?

192.168.2.333
292.168.2.1

Because every number represents one byte of the 32-bit IP address and a byte can only hold numbers between 0 and 255.

1 Like

thanks man... you made my day :smiley:

I am really really grateful :upside_down_face:

Note that I edited the functions to reject stuff like "192.168..1" and "192.168.02.1".

1 Like

okay, I really thank you man :smiley:

greetings fron Germany, Bielefeld

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.