Hi,
My programme has a text box where in user enters the ip address. I need to validate the IP (like no space allowed in the entry, the numerals should not exceed 255, not more than 3 dots allowed overall, no consecutive dots allowed etc.,.). How do i go about the same? Is there any built in function for the same?
thanks,
Badri
5 29143 bvdet 2,851
Expert Mod 2GB
Hi,
My programme has a text box where in user enters the ip address. I need to validate the IP (like no space allowed in the entry, the numerals should not exceed 255, not more than 3 dots allowed overall, no consecutive dots allowed etc.,.). How do i go about the same? Is there any built in function for the same?
thanks,
Badri
Badri,
This may cover most of of your requirements: - ip_str = "23.34.45.45"
-
-
def ipFormatChk(ip_str):
-
if len(ip_str.split()) == 1:
-
ipList = ip_str.split('.')
-
if len(ipList) == 4:
-
for i, item in enumerate(ipList):
-
try:
-
ipList[i] = int(item)
-
except:
-
return False
-
if not isinstance(ipList[i], int):
-
return False
-
if max(ipList) < 256:
-
return True
-
else:
-
return False
-
else:
-
return False
-
else:
-
return False
-
-
print ipFormatChk(ip_str)
-
True
Nice coding, BV!
Badri,
This may cover most of of your requirements: - ip_str = "23.34.45.45"
-
-
def ipFormatChk(ip_str):
-
if len(ip_str.split()) == 1:
-
ipList = ip_str.split('.')
-
if len(ipList) == 4:
-
for i, item in enumerate(ipList):
-
try:
-
ipList[i] = int(item)
-
except:
-
return False
-
if not isinstance(ipList[i], int):
-
return False
-
if max(ipList) < 256:
-
return True
-
else:
-
return False
-
else:
-
return False
-
else:
-
return False
-
-
print ipFormatChk(ip_str)
-
True
You can also use regular expressions like this -
import re
-
ip_str = "23.34.45.45"
-
-
def ipFormatChk(ip_str):
-
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
-
if re.match(pattern, ip_str):
-
return True
-
else:
-
return False
-
print ipFormatChk(ip_str)
-
bvdet 2,851
Expert Mod 2GB
You can also use regular expressions like this -
import re
-
ip_str = "23.34.45.45"
-
-
def ipFormatChk(ip_str):
-
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
-
if re.match(pattern, ip_str):
-
return True
-
else:
-
return False
-
print ipFormatChk(ip_str)
-
Regular expressions are ideal for this application. This code executes much faster than the other. The drawback is readability. An alternative is to set the VERBOSE flag and add comments: - pattern = re.compile(r"""
-
\b # matches the beginning of the string
-
(25[0-5]| # matches the integer range 250-255 OR
-
2[0-4][0-9]| # matches the integer range 200-249 OR
-
[01]?[0-9][0-9]?) # matches any other combination of 1-3 digits below 200
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\b # matches the end of the string
-
""", re.VERBOSE)
It executes slower, but I guess you can't have everything! Thanks for posting Mike.
Regular expressions are ideal for this application. This code executes much faster than the other. The drawback is readability. An alternative is to set the VERBOSE flag and add comments: - pattern = re.compile(r"""
-
\b # matches the beginning of the string
-
(25[0-5]| # matches the integer range 250-255 OR
-
2[0-4][0-9]| # matches the integer range 200-249 OR
-
[01]?[0-9][0-9]?) # matches any other combination of 1-3 digits below 200
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\. # matches '.'
-
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
-
\b # matches the end of the string
-
""", re.VERBOSE)
It executes slower, but I guess you can't have everything! Thanks for posting Mike.
I like you style, BV.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: michaaal |
last post by:
I realize this is not solely an ASP question, but I thought you guys might
know the answer and I couldn't find anywhere else to post this. If you have
suggestions for this I am more than happy to...
|
by: Dag Sunde |
last post by:
My understanding of regular expressions is rudimentary,
at best.
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses...
|
by: Tony Nelson |
last post by:
I'd like to have a fast way to validate large amounts of string data as
being UTF-8.
I don't see a fast way to do it in Python, though:
unicode(s,'utf-8').encode('utf-8)
seems to notice at...
|
by: Dave |
last post by:
Hello.
Is there any method to validate if string contains valid IP address?
I also need the method to valideta email address in string.
Thank you.
|
by: Wm. Scott Miller |
last post by:
Hello all:
I'd like some advice on the best way to validate and confirm an e-mail
address entered during a registration process. What we are thinking of is
something like the following:
1. ...
|
by: Reid Priedhorsky |
last post by:
Hi folks,
I have a need to validate XML files against both DTDs and XML Schema from
the command line.
In an ideal world, I'd be able to do something like:
$ python validate.py foo.xml
...
|
by: codefire |
last post by:
Hi,
I am trying to get a regexp to validate email addresses but can't get
it quite right. The problem is I can't quite find the regexp to deal
with ignoring the case james..kirk@fred.com, which...
|
by: SkipNRun |
last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list.
...
|
by: saravanatmm |
last post by:
I need javascript code for validate the email address.
Email address field cannot allowed the capital letters, special characters except '@' symbol.
But can allowed the small letters, numeric...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |