[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

JavaScript Expression

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
senthil_krn
Asked:
senthil_krn
  • 3
  • 2
  • 2
  • +6
1 Solution
 
djokovCommented:
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
 
dash420Commented:
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
 
hualingCommented:
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
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!

 
dash420Commented:
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
 
MHQCommented:
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
 
pavlinCommented:
I think djokov's answer is the most correct...
0
 
senthil_krnAuthor Commented:
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
 
senthil_krnAuthor Commented:
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
 
senthil_krnAuthor Commented:
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
 
djokovCommented:
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
 
MHQCommented:
Some feedback senthil?
0
 
MoondancerCommented:
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
 
COBOLdinosaurCommented:
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
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now