Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Validate that an IP address has been entered into a textfield

Posted on 2006-04-11
12
Medium Priority
?
282 Views
Last Modified: 2012-06-21
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!
0
Comment
Question by:clonmelog
10 Comments
 
LVL 6

Expert Comment

by:nabsol
ID: 16428046
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
 
LVL 6

Expert Comment

by:nabsol
ID: 16428057
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
 
LVL 2

Author Comment

by:clonmelog
ID: 16428083
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:nabsol
ID: 16428134
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
 
LVL 6

Expert Comment

by:nabsol
ID: 16428161
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
 
LVL 6

Expert Comment

by:nabsol
ID: 16428306
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
 
LVL 4

Expert Comment

by:ysre
ID: 16429453
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
 
LVL 6

Accepted Solution

by:
nabsol earned 500 total points
ID: 16429533
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16431591
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
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 500 total points
ID: 16431718
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question