Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IP validation ?

Posted on 2003-10-22
9
Medium Priority
?
969 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nicolle
ID: 9604214
Yes, 255.255.255.255 is invalid
0
 
LVL 16

Accepted Solution

by:
jaysolomon earned 150 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

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 Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

824 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