Link to home
Start Free TrialLog in
Avatar of senthil_krn
senthil_krn

asked on

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
Avatar of djokov
djokov

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;
}
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.


   
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
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.
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);
I think djokov's answer is the most correct...
Avatar of senthil_krn

ASKER

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
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
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
ASKER CERTIFIED SOLUTION
Avatar of djokov
djokov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some feedback senthil?
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.
https://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.
https://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
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20119946



Moondancer
Moderator @ Experts Exchange
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&
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange