Validate that an IP address has been entered into a textfield

Hi,

I have the following form which is taking in IP addresses:

<form name="form1" method="post" action="ban.php">
  IP to ban:
  <input name="ip" type="text" id="ip">
  <br>
  <input type="submit" name="Submit" value="Submit">
  <br>
  <br>
</form>

What I want to do is to validate that the input is an IP address and if not send back a popup to screen for the user. obviously an IP cant contain letters and must have the four dots between the numbers.

how is this done?

thanks!
LVL 2
clonmelogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nabsolCommented:
Hi

This will help. You can use that code
http://javascript.internet.com/forms/val-ip.html

or

http://www.irt.org/script/852.htm

By Nab
0
nabsolCommented:
Hi

Code directly:


<!-- TWO STEPS TO INSTALL VALIDATION (IP ADDRESS):

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Jay Bienvenu -->
<!-- Web Site:  http://www.bienvenu.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.");
else
alert (errorString);
}
//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<form>
IP Address:
<input size=15 name="IPvalue">
<input type="submit" value="Verify" onClick="verifyIP(IPvalue.value)";>
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

By Nab
0
clonmelogAuthor Commented:
Ive seen the second one before but it doesnt really solve my problem fully.

I want them to click submit and if it does pass validation then goes onto the next page which the form in my above post is directed at.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nabsolCommented:
Hi
So this will solve your problem . The code :)

<!-- TWO STEPS TO INSTALL VALIDATION (IP ADDRESS):

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Jay Bienvenu -->
<!-- Web Site:  http://www.bienvenu.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
{
  alert ("That is a valid IP address.");
  return true;
}
else
{
  alert (errorString);
  return false;
}
}
//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<form>
IP Address:
<input size=15 name="IPvalue">
<input type="submit" value="Verify" onClick="return verifyIP(IPvalue.value)";>
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>


By Nab
0
nabsolCommented:
Hi
So just use this code which is written this function "verifyIP" will return true or false and use onsubmit="return verifyIP(IPvalue.value)"

Hope this is what u want

By Nab
0
nabsolCommented:
Hi
Dont worry just use this code:

<html>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function verifyIP (IPvalue) {

errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
{
  alert ("That is a valid IP address.");
  return true;
}
else
{
  alert (errorString);
  return false;
}
}
//  End -->
</script>

</HEAD>



<body>

<form name="form1" method="post" action="ban.php" onsubmit="return verifyIP(ip.value)">
  IP to ban:<input name="ip" type="text" id="ip"><input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

This works fine for you.

By Nab

0
ysreCommented:
I usually end up creating an <input type="button" onClick="verifyThis()"> which calls document.formname.submit(); if everything verifies OK.
Avoids the (sometimes) browser weirdness <form onSubmit="foo()">.
0
nabsolCommented:
Hi
I think this is the actual way of doing thing.

<body>
<form name="form1" method="post" action="ban.php" onsubmit="return verifyIP(ip.value)">
  IP to ban:<input name="ip" type="text" id="ip"><input type="submit" name="Submit" value="Submit">
</form>
</body>

and this code is exactly what clonmelog wants :)

///////////////


<html>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function verifyIP (IPvalue) {

errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
{
  alert ("That is a valid IP address.");
  return true;
}
else
{
  alert (errorString);
  return false;
}
}
//  End -->
</script>

</HEAD>



<body>

<form name="form1" method="post" action="ban.php" onsubmit="return verifyIP(ip.value)">
  IP to ban:<input name="ip" type="text" id="ip"><input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


/////////////

By Nab
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZvonkoSystems architectCommented:
Does this help:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function checkField(e){
  var theField = (e.target)?e.target:e.srcElement;
  if(theField.name.indexOf("IP")==0){
    if(theField.value.length==3||theField.value.indexOf(".")>=0){
      if(theField.value==".") theField.value=0;
      theField.value = theField.value.replace(/\D/g,"");
      if(theField.value*1>255){
        alert("Not an IP number: "+theField.value);
        theField.value="";
        theField.focus();
        return;
      }
      var elem = theField.form.elements;
      for(var i=0;i<elem.length&&elem[i]!=theField;i++);
      if(i==elem.length) i=0; else i++;
      for(;i<elem.length&&elem[i]!=theField;i++){
        if(elem[i].name&&elem[i].name.indexOf("IP")==0){
          elem[i].focus();
          return;
        }
      }
    }
  }
}
</script>
</head>
<body>
<form onKeyUp="checkField(event)">
IP1:
<input type="text" name="IP1S1" size="3" maxlength="3">.
<input type="text" name="IP1S2" size="3" maxlength="3">.
<input type="text" name="IP1S3" size="3" maxlength="3">.
<input type="text" name="IP1S4" size="3" maxlength="3"><br>
IP2:
<input type="text" name="IP2S1" size="3" maxlength="3">.
<input type="text" name="IP2S2" size="3" maxlength="3">.
<input type="text" name="IP2S3" size="3" maxlength="3">.
<input type="text" name="IP2S4" size="3" maxlength="3"><br>
IP3:
<input type="text" name="IP3S1" size="3" maxlength="3">.
<input type="text" name="IP3S2" size="3" maxlength="3">.
<input type="text" name="IP3S3" size="3" maxlength="3">.
<input type="text" name="IP3S4" size="3" maxlength="3"><br>
</form>
</body>
</html>

0
ZvonkoSystems architectCommented:
Or more something like this:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function checkForm(theForm){
  var ip = theForm.ip.value.split(".");
  if(ip.length==4){
    for(var i=0;i<4;i++){
      if(ip[i].match(/\D/)||ip[i]*1>255){
        alert("Not an IP byte: "+ip[i]);
        theForm.ip.select();
        theForm.ip.focus();
        return false;
      }
    }
    return true;
  }
  alert("Enter a valid IP address.");
  theForm.ip.select();
  theForm.ip.focus();
  return false;
}
</script>
</head>
<body>
<form name="form1" method="post" action="ban.php" onSubmit="return checkForm(this)">
  IP to ban:
  <input name="ip" type="text" id="ip" size="15" maxLength="15" style="font-family: Courier;">
  <br>
  <input type="submit" name="Submit" value="Submit">
  <br>
  <br>
</form>
</body>
</html>


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.