Can I verify ip address 192.168.120.1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas251074
    New Member
    • Dec 2007
    • 198

    Can I verify ip address 192.168.120.1

    I have developed form for assigning ip address to computers, printers, routers, switch, camera and ip phone. When a user enter ip address like 192.168.120.1, Here, there are four parts of ip address 192, 168, 120 and 1. Can I verify each part within the range of 0-255? How can this be verified?
    And also that all four part must be entered.

    Thanks and regards,
    Vikas
  • veenna
    New Member
    • Dec 2007
    • 65

    #2
    First store that ip address in one variable 'str'

    try this function


    Code:
    <script type="text/javascript">
    function SplitStr(thiss)
    {
    var str=thiss.value;
    var i=0;
    var dot= new Array();
    	dot=str.split('.');
    
    for(i=0;i<=dot.length-1;i++)
    {
    if(dot[i]<=0 || dot[i]>=255)
    {
    alert('num should be between 0 to 255');
    }
    }
    }
    </script>

    call this function like:


    Code:
    <input type="text" name="txtIp" id="txtIp" onblur="SplitStr(this);"/>

    Comment

    • vikas251074
      New Member
      • Dec 2007
      • 198

      #3
      Yes madam, you are great.

      It is working fine. Here only little modification requires. If found out of range, then cursor should remain on same field. So I added line
      Code:
      document.myform.vlan_first_ip.focus();
      after the alert message.

      But due to adding above line, I can not use splitstr() for two more fields which goes through the same process.

      I don't want to create more than one function which have same process but different fields. Can I do this?

      Thanks and regards,
      Vikas

      Comment

      • veenna
        New Member
        • Dec 2007
        • 65

        #4
        vikas,


        I think instead of this line

        Code:
        document.myform.vlan_first_ip.focus();

        you can give thiss.focus(); after alert message.



        regards
        Veenna

        Comment

        • vikas251074
          New Member
          • Dec 2007
          • 198

          #5
          No madam, this is not working.

          Thanks and regards,
          Vikas

          Comment

          • vikas251074
            New Member
            • Dec 2007
            • 198

            #6
            if first ip = 192.168.120.1 is given by user, then last ip can automatically assigned as 192.168.120.255 .

            For this I have written following code, but value is not assigned.
            Code:
            function firstip(temp){
              var str=temp.value;
              var i=0;
              var dot= new Array();
              dot=str.split('.');
              for(i=0;i<=dot.length-1;i++){
                if(dot[i]<=0 || dot[i]>=255){
                  document.myform.first_ip.focus();
                  alert('IP should be in the range 0 to 255');
                }
              }
              document.myform.last_ip.value = temp.substr(0, temp.lastIndexOf('.'))+255;
            }

            Comment

            • veenna
              New Member
              • Dec 2007
              • 65

              #7
              Vikas,


              Are sure that the user will enter .1 in first ip

              if yes, you can directly use

              Code:
              var str1=str.substring(0,str.lastIndexOf('.'));
              document.getElementById('Text1').value =str1 + '.' + 255;

              Comment

              • vikas251074
                New Member
                • Dec 2007
                • 198

                #8
                I tried this, but not working. Value is not assigned to last_ip. Even alert message is also not displaying.
                The function where code is written is follows -

                Code:
                function firstip(temp){
                  var str=temp.value;
                  var i=0;
                  var dot= new Array();
                  dot=str.split('.');
                  for(i=0;i<=dot.length-1;i++){
                    if(dot[i]<=0 || dot[i]>=255){
                      alert('IP should be in the range 0 to 255');
                    }
                  }
                  var str1 = str.substring(0, lastIndexOf('.'));
                  alert(str1);
                  document.getElementById('lastip').value = str1 + '.' + 255;
                }
                And where the function is called is as follows -
                Code:
                	    <table>
                	      <tr>
                		    <td align="right" style="width:125px ">VLAN Name :</td>
                		    <td align="left"><input type="text" style="width:250px " name="vlan_name" id="vlan_name" onkeyup="capital(this);"/></td>
                		  </tr>
                		  <tr>
                		    <td align="right" style="width:125px ">VLAN First IP :</td>
                		    <td align="left"><input type="text" style="width:100px " name="first_ip" id="first_ip" onchange="firstip(this);"/></td>
                          </tr>
                          <tr>
                            <td align="right" style="width:125px ">VLAN Last IP :</td>
                		    <td align="left"><input type="text" style="width:100px " name="last_ip" id="last_id"/></td>
                          </tr>
                          <tr>
                            <td align="right" style="width:125px ">VLAN Subnet Mask :</td>
                            <td align="left"><input type="text" style="width:100px " name="subnet_mask"/></td>
                          </tr>
                        </table>
                Thanks and regards,
                Vikas

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Notice on line 12, you have:
                  Code:
                  <td align="left"><input type="text" style="width:100px " name="last_ip" id="last_id"/></td>
                  The "last_id" should obviously be "last_ip". A good old typo.

                  Comment

                  • vikas251074
                    New Member
                    • Dec 2007
                    • 198

                    #10
                    Thanks for find such an error. Still it is not assigning value to 'last_ip' field.

                    Now I changed to code like this
                    Code:
                    function firstip(temp){
                      var str=temp.value;
                      var i=0;
                      var dot= new Array();
                      dot=str.split('.');
                      for(i=0;i<=dot.length-1;i++){
                        if(dot[i]<=0 || dot[i]>=255){
                          alert('IP should be in the range 0 to 255');
                        }
                      }
                      var str1 = str.substring(0, lastIndexOf('.'));
                      document.getElementById('last_ip').value = str1 + '.' + 255;
                    }
                    And calling portion of HTML script are -
                    Code:
                        <table>
                     <tr>
                        <td align="right" style="width:125px ">VLAN Name :</td>
                        <td align="left"><input type="text" style="width:250px " name="vlan_name" id="vlan_name" onkeyup="capital(this);"/></td>
                      </tr>
                      <tr>
                        <td align="right" style="width:125px ">VLAN First IP :</td>
                        <td align="left"><input type="text" style="width:100px " name="first_ip" id="first_ip" onchange="firstip(this);"/></td>
                      </tr>
                      <tr>
                        <td align="right" style="width:125px ">VLAN Last IP :</td>
                        <td align="left"><input type="text" style="width:100px " name="last_ip" id="last_ip"/></td>
                      </tr>
                      <tr>
                        <td align="right" style="width:125px ">VLAN Subnet Mask :</td>
                        <td align="left"><input type="text" style="width:100px " name="subnet_mask"/></td>
                      </tr>
                    </table>
                    Thanks and regards,
                    Vikas

                    Comment

                    • vikas251074
                      New Member
                      • Dec 2007
                      • 198

                      #11
                      Yes sir, I got it. Thanks everybody for help.
                      Error was in line no. 11 of function description.
                      Code:
                      var str1 = str.substring(0, lastIndexOf('.'));
                      silly mistake was 'str' was not added to lastIndexOf('.' )

                      Thanks and regards
                      Vikas

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Yes, it should've been str.lastIndexOf . I'm sure your error console would've told you that, i.e. that lastIndexOf is undefined. Remember to check if you've got errors.

                        Glad you've got it working.

                        Comment

                        • vikas251074
                          New Member
                          • Dec 2007
                          • 198

                          #13
                          Can I check first three parts of IP address like 192.168.120 if exist in oracle table, If yes, then how?

                          Thanks and regards,
                          Vikas

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            That would have to be done using server-side code (ASP in your case). JavaScript cannot directly access an Oracle database, though you can use Ajax to make a request to an ASP script.

                            Comment

                            • vikas251074
                              New Member
                              • Dec 2007
                              • 198

                              #15
                              But sir I have to verify IP address without submitting form from oracle database, so how can I use ASP.

                              Comment

                              Working...