Solved

IP validation ?

Posted on 2003-10-22
9
957 Views
Last Modified: 2011-10-03
How do I use Javascript to do IP validation ? The IP validation will validate 255.255.255.255 or 0.x.x.x as invalidate IP. Any sample ?
0
Comment
Question by:nicolle
  • 3
  • 3
  • 3
9 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9604073
this?

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--Begin
function isIPaddy(strValue){
                              // 255  .    255  . 255  . 255  
      var objRegExp = /^\d{3}\.{1}\d{3}\.\d{3}\.\d{3}$/
      return objRegExp.test(strValue);
}
function validateForm(fObj){
      if(!isIPaddy(fObj["ip"].value)){
            alert("Please enter a correct IP address\n\nEx. 255.255.255.255");
            fObj["ip"].select();
            return false;
            }
      return true;
}
             
//End-->
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="return validateForm(this);">
  <input type="text" name="ip">
  <input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
0
 

Author Comment

by:nicolle
ID: 9604104
your code does not accept 1.1.1.1.

Sorry ! I should state clear to you. In my case, 255.255.255.255 and first part begin
with 0 are invalid.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9604187
ok so

 var objRegExp = /^[1-9]{1,3}\.{1}\d{1,3}\.\d{1,3}\.\d{1,3}$/

is 255.255.255.255 invalid
0
 

Author Comment

by:nicolle
ID: 9604214
Yes, 255.255.255.255 is invalid
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 16

Accepted Solution

by:
jaysolomon earned 50 total points
ID: 9606263
This maybe

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--Begin
function isIPaddy(strValue){
      var objRegExp = /^[1-9]{1,3}\.{1}\d{1,3}\.\d{1,3}\.\d{1,3}$/
    return objRegExp.test(strValue);
}
function validateForm(fObj){
      if(fObj["ip"].value == "255.255.255.255"){
            alert("The IP Address 255.255.255.255 is not a valid IP address.");
            fObj["ip"].select();
            return false;
    }
    if(!isIPaddy(fObj["ip"].value)){
            alert("Please enter a correct IP address\n\nEx. 255.255.255.255");
            fObj["ip"].select();
            return false;
      }
    return true;
}
           
//End-->
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="return validateForm(this);">
  <input type="text" name="ip">
  <input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
0
 
LVL 3

Expert Comment

by:makc
ID: 9608295
:))) LOL

if(fObj["ip"].value == "255.255.255.255")

<-- That was funny !

I would double points for that!
0
 

Author Comment

by:nicolle
ID: 9617495
makc,

You have better regular expression on that ?
0
 
LVL 3

Expert Comment

by:makc
ID: 9619721
actually, i don't see why to use regexp at all. make four inpus for each byte, use isNaN(input.value) and check it to be in 1...254 range. that's it, isn't it?
0
 
LVL 3

Expert Comment

by:makc
ID: 9619729
isNaN(parseInt(input.value)), maybe, but you got the picture.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now