Solved

JavaScript Expression

Posted on 2001-07-18
14
278 Views
Last Modified: 2006-11-17
Hi,
    I want to write the JavaScript Regular Expersion for the Data Validation.
    What I need, is the data will be a IP address (i.e) "x.x.x.x"
    and x should not be more than 3 digit number.
    And the data should not be "10.x.x.x" and "192.168.x.x".
    Can any one help me on this.
    Thanks,
    Senthil
0
Comment
Question by:senthil_krn
  • 3
  • 2
  • 2
  • +6
14 Comments
 
LVL 2

Expert Comment

by:djokov
Comment Utility
function isIP (s) {
     reIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
     if (reIP.test(s) &&
          (Number(RegExp.$1) < 256) &&
          (Number(RegExp.$2) < 256) &&
          (Number(RegExp.$3) < 256) &&
          (Number(RegExp.$4) < 256))
          return true;
     return false;
}
0
 
LVL 6

Expert Comment

by:dash420
Comment Utility
I don't know why not 192.168.x.x, is it is particular to u required. actually i don't know how will the ip address format. tell me details.


   
0
 

Expert Comment

by:hualing
Comment Utility
Senthil
I don't think regular expression can cover all situation
here is a example of RE for you reference:

ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
you should add some more script for the special 0.0.0.0 or 255.255.255.255 or any number which is more than 255....
 
mimi
0
 
LVL 6

Expert Comment

by:dash420
Comment Utility
if this case, ip address x.x.x.x

and each should not be greater than 255, as such there no format.if u put 192.256.245,.2 it will convert it into
192.0.245,.2  

so here is the validation

 <script language="JavaScript">
       function validateform()
        {
       var i    
          var str = document.Form1.USER_NAME.value      
       var test = str.split('.',4)
       for( i=0; i <= test.length ; i++){
         if (test[i].length > 3){
         alert("should not be more than 3 digit")
         return false
       }
     }
 </script>

I am blocking the question so that u can reject if u are not satify with my answer.
0
 
LVL 3

Expert Comment

by:MHQ
Comment Utility
Maybe not as neat as the others but working as you asked for, including the IPs you mentioned and a check to see that it isn't a mask (255).

<SCRIPT language='javascript'>

function valIP(IPno)  
{
flag=true;
parts=IPno.split(".");
if (parts[0] == 10)
 {
  flag=false;
 }
if ((parts[0] == 192) && (parts[1]==168 ))
 {
  flag=false;
 }
for (a=0;a<parts.length;a++)
 {
  if ( (parts[a].length > 3) || (parts[a] > 254) )
   {
    flag=false;
   }
 }
if (!flag)
 {
  document.write("Error!");
 }
}
</script>

you call it using valIP(x.x.x.x);
0
 

Expert Comment

by:pavlin
Comment Utility
I think djokov's answer is the most correct...
0
 

Author Comment

by:senthil_krn
Comment Utility
Hi djokov,
I have the expression like this
((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                    + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\.((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                    + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])

but i should not allow "10.x.x.x" and "192.168.x.x" Ip Address can you pls tell me how to do this.
Thanks,
Senthil
0
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.

 

Author Comment

by:senthil_krn
Comment Utility
Hi djokov,
I have the expression like this
((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                    + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\.((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                    + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])

but i should not allow "10.x.x.x" and "192.168.x.x" Ip Address can you pls tell me how to do this.
Thanks,
Senthil
0
 

Author Comment

by:senthil_krn
Comment Utility
Hi djokov,
I have the expression like this
((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                   + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\.((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])\\."
                   + "((2[0-4][0-9])|(2[0-5][0-5])|(1[0-9][0-9])|([1-9][0-9])|[0-9])

but i should not allow "10.x.x.x" and "192.168.x.x" Ip Address can you pls tell me how to do this.
Thanks,
Senthil
0
 
LVL 2

Accepted Solution

by:
djokov earned 50 total points
Comment Utility
function isIP (s) {
 reIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
 if (reIP.test(s) &&
  (Number(RegExp.$1) != 10) &&
  (Number(RegExp.$1) != 192 && Number(RegExp.$2) != 168) &&
  (Number(RegExp.$1) < 256) &&
  (Number(RegExp.$2) < 256) &&
  (Number(RegExp.$3) < 256) &&
  (Number(RegExp.$4) < 256))
  return true;
return false;
}

When you use () in your Reg Exp pattern and then test() the string to this regexp, you will get all data you put in () in the RegExp.$1 to RegExp.$9 variables. So I use this to check IP addresses you like to skip.

Cheers.
0
 
LVL 3

Expert Comment

by:MHQ
Comment Utility
Some feedback senthil?
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
This question appears to have been abandoned. In fact, I will include links to ALL your open questions below:
 
Your options are:
1.  Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3.  Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4.  Delete the question. Again, you must tell the other participants why you wish to do this.
 
For special handling needs, please post a zero point question in the link below, include the question QID/link.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues.
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
 

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:
 
Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
 
Thank you everyone.
 
Also still open
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20119946



Moondancer
Moderator @ Experts Exchange
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
It is time to clean this abandoned question up.  

I am putting it on a clean up list for CS.

<recommendation>
points to djokov

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now