Solved

IP validation ?

Posted on 2003-10-22
9
960 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:nicolle
ID: 9604214
Yes, 255.255.255.255 is invalid
0
 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

825 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