Solved

IP validation ?

Posted on 2003-10-22
9
961 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

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!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

763 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