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 29141 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: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |