Solved

JavaScript Expression

Posted on 2001-07-18
14
281 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
ID: 6294268
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
ID: 6294305
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
ID: 6294316
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:dash420
ID: 6294557
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
ID: 6294604
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
ID: 6294738
I think djokov's answer is the most correct...
0
 

Author Comment

by:senthil_krn
ID: 6296180
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
ID: 6296184
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
ID: 6296644
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
ID: 6297224
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
ID: 6345689
Some feedback senthil?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6828859
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
ID: 7999845
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
ID: 8068284
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
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…

809 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