Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 864987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:34:09+00:00 2026-05-15T09:34:09+00:00

I am using this IP Validation Function that I came across while browsing, it

  • 0

I am using this IP Validation Function that I came across while browsing, it has been working well until today i ran into a problem.

For some reason the function won’t validate this IP as valid: 203.81.192.26

I’m not too great with regular expressions, so would appreciate any help on what could be wrong.

If you have another function, I would appreciate if you could post that for me.

The code for the function is below:

public static function validateIpAddress($ip_addr)
{
    global $errors;

    $preg = '#^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' .
            '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$#';

    if(preg_match($preg, $ip_addr))
    {
        //now all the intger values are separated
        $parts = explode(".", $ip_addr);

        //now we need to check each part can range from 0-255
        foreach($parts as $ip_parts)
        {
            if(intval($ip_parts) > 255 || intval($ip_parts) < 0)
            {
                $errors[] = "ip address is not valid.";
                return false;
            }

            return true;
        }

        return true;
    } else {
        $errors[] = "please double check the ip address.";
        return false;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:34:10+00:00Added an answer on May 15, 2026 at 9:34 am

    Well, why are you doing both regex and int comparisons? You are “double” checking the address. Also, your second check is not valid, as it will always return true if the first octet is valid (you have a return true inside of the foreach loop).

    You could do:

    $parts = explode('.', $ip_addr);
    if (count($parts) == 4) {
        foreach ($parts as $part) { 
            if ($part > 255 || $part < 0) {
                //error
            }
        }
        return true;
    } else {
        return false;
    }
    

    But as others have suggested, ip2long/long2ip may suit your needs better…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this the only way to create custom model validation? To do it using
I am using databinding and IDataErrorInfo style validation in a form. This form includes
I am currently using $.blur(fn();) for form validation, but this only takes effect when
Using this C# application pick a start and ending date range, that SAP will
hope you can help me on this one, I'm currently using this: jQuery plugin:validation
I'm working on a jQuery function that forwards form data to page without interfering
I have a function that contains messages for form field validation. However I would
I'm having one heck of a time getting this validation to work. I'm using
Possible Duplicate: Javascript date regex DD/MM/YYYY i'm using this validation :- jquery validation engine
I'm using this jQuery validation plugin and getting the famous: $(#Form).validate is not a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.