Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching problem 1

Status
Not open for further replies.

carlosAlberto

IS-IT--Management
Oct 9, 2001
109
0
0
GB
Hi all,

I have a piece of code that checks if a value entered is a valid IP address. I split the address into four part, which each part should be from 1 - 254.

This is the code where i actually check the value:

if (thisSegment < &quot;0&quot; || thisSegment > &quot;254&quot;) {


I have an alert box before this line and checked the value.

The problem arises when i enter 3.3.3.3 4.4.4.4 ... up to 9.9.9.9 all other value are check correctly.

The alert box shows thisSegment holds the value 3. This loops through 4 times. If i remove the OR statement it seems to work (just checking greater then 0).

Puzzled....

Is there something wrong with the above.
 
this could be done so much more effectivly with a regular expression filter. try that approach instead.

eg
function checkIP() {
var x = document.forms[0].IP.value;
var filter = /^\d+\.\d+\.\d+\.\d+$/;
if (filter.test(x)) {
alert('YES! Correct ip address');
} else {
alert('NO! Incorrect ip address');
}
} _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
here's the form section I tested it with incase you have trouble getting it to work you can see with this
IP<br>
<input type=&quot;text&quot; name=&quot;IP&quot; onBlur=&quot;checkIP()&quot;>

_________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
onpnt,

your regex will allow an IP such as &quot;12345.67890.9999999.7890978079879807970970798790&quot;

a non-regex solution isn't that cumbersome in this case:

[tt]
<html>
<head>
<script language=&quot;javascript&quot;>
function checkIP(s) {
bad = false;
s = s.split(&quot;.&quot;);
if (s.length != 4) {
bad = true;
}
else {
for (x = 0; x < 4; x++) {
if (s[x] < 0 || s[x] > 255 || !s[x].length) {
bad = true;
}
}
}
if (bad) {
alert(&quot;invalid ip&quot;);
return false;
}
else return true;
}
</script>
</head>
<body>
<form>
<input type=&quot;text&quot; name=&quot;ip&quot; />
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;c&quot; onclick=&quot;checkIP(this.form.ip.value);&quot; />
</form>
</body>
</html>
[/tt]
=========================================================
if (!succeed) try();
-jeff
 
/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
still will allow 256 - 999
:) =========================================================
if (!succeed) try();
-jeff
 
well, I'm giving up on this one unless someone has a saving pattern for me. you could go into pattern matching for
[0-9] [0-2] etc. but that would be way less efficient then what jemminger has already given you.
I give a star for taking the considerations of my suggestion further and proving a better solution.
that's the best answers you can give!! _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
thanks onpnt...btw your new site looks great! =========================================================
if (!succeed) try();
-jeff
 
thanks jeff, I finally had some time to work on it. some functional issues still but that's waiting for the next spert of free time :) _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
Thanks Guys,

I've implemented the answer Jemminger gave and works fine.

However.........


There is still a problem. It accepts characters.... (I presume it's taking the ascii value if between 1-255).

Is there a function to check for an integer ????


Thanks again.

Carl.
 
oops...try this:
[tt]
<script language=&quot;javascript&quot;>
function checkIP(s) {
bad = false;
s = s.split(&quot;.&quot;);
if (s.length != 4) {
bad = true;
}
else {
for (x = 0; x < 4; x++) {
n = parseInt(s[x],10);
if (n < 0 || n > 255 || isNaN(n)) {
bad = true;
}
}
}
if (bad) {
alert(&quot;invalid ip&quot;);
return false;
}
else return true;
}
</script>
[/tt] =========================================================
if (!succeed) try();
-jeff
 
Thanks.


Can you briefly explain this line (what does the 10 signify):

n = parseInt(s[x],10);

Does this mean i can remove this line now???

!s[x].length

from the condition if... || ... ||


Carl.
 
hi Carl,

the method [tt]parseInt(sNumber[, iBase])[/tt] takes two parameters: sNumber is the string to convert to an integer, and iBase is an optional parameter specifying the base of the number to parse, 10 being decimal. (16 = hex, 8 = octal, etc...)

by default, parseInt will assume the base is 10 if sNumber begins with 1-9, but if you pass a number beginning in zero and do not specify a base of 10, it will be parsed as octal.


!s[x].length
should be replaced with
isNaN(n) =========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top