By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,514 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,514 IT Pros & Developers. It's quick & easy.

Can I verify ip address 192.168.120.1

vikas251074
100+
P: 198
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
Jun 7 '08 #1
Share this Question
Share on Google+
19 Replies


P: 65
First store that ip address in one variable 'str'

try this function


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function SplitStr(thiss)
  3. {
  4. var str=thiss.value;
  5. var i=0;
  6. var dot= new Array();
  7.     dot=str.split('.');
  8.  
  9. for(i=0;i<=dot.length-1;i++)
  10. {
  11. if(dot[i]<=0 || dot[i]>=255)
  12. {
  13. alert('num should be between 0 to 255');
  14. }
  15. }
  16. }
  17. </script>

call this function like:


Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="txtIp" id="txtIp" onblur="SplitStr(this);"/>
Jun 7 '08 #2

vikas251074
100+
P: 198
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
Expand|Select|Wrap|Line Numbers
  1. 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
Jun 7 '08 #3

P: 65
vikas,


I think instead of this line

Expand|Select|Wrap|Line Numbers
  1. document.myform.vlan_first_ip.focus();

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



regards
Veenna
Jun 7 '08 #4

vikas251074
100+
P: 198
No madam, this is not working.

Thanks and regards,
Vikas
Jun 7 '08 #5

vikas251074
100+
P: 198
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.
Expand|Select|Wrap|Line Numbers
  1. function firstip(temp){
  2.   var str=temp.value;
  3.   var i=0;
  4.   var dot= new Array();
  5.   dot=str.split('.');
  6.   for(i=0;i<=dot.length-1;i++){
  7.     if(dot[i]<=0 || dot[i]>=255){
  8.       document.myform.first_ip.focus();
  9.       alert('IP should be in the range 0 to 255');
  10.     }
  11.   }
  12.   document.myform.last_ip.value = temp.substr(0, temp.lastIndexOf('.'))+255;
  13. }
  14.  
  15.  
Jun 7 '08 #6

P: 65
Vikas,


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

if yes, you can directly use

Expand|Select|Wrap|Line Numbers
  1. var str1=str.substring(0,str.lastIndexOf('.'));
  2. document.getElementById('Text1').value =str1 + '.' + 255;
Jun 7 '08 #7

vikas251074
100+
P: 198
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 -

Expand|Select|Wrap|Line Numbers
  1. function firstip(temp){
  2.   var str=temp.value;
  3.   var i=0;
  4.   var dot= new Array();
  5.   dot=str.split('.');
  6.   for(i=0;i<=dot.length-1;i++){
  7.     if(dot[i]<=0 || dot[i]>=255){
  8.       alert('IP should be in the range 0 to 255');
  9.     }
  10.   }
  11.   var str1 = str.substring(0, lastIndexOf('.'));
  12.   alert(str1);
  13.   document.getElementById('lastip').value = str1 + '.' + 255;
  14. }
And where the function is called is as follows -
Expand|Select|Wrap|Line Numbers
  1.         <table>
  2.           <tr>
  3.             <td align="right" style="width:125px ">VLAN Name :</td>
  4.             <td align="left"><input type="text" style="width:250px " name="vlan_name" id="vlan_name" onkeyup="capital(this);"/></td>
  5.           </tr>
  6.           <tr>
  7.             <td align="right" style="width:125px ">VLAN First IP :</td>
  8.             <td align="left"><input type="text" style="width:100px " name="first_ip" id="first_ip" onchange="firstip(this);"/></td>
  9.           </tr>
  10.           <tr>
  11.             <td align="right" style="width:125px ">VLAN Last IP :</td>
  12.             <td align="left"><input type="text" style="width:100px " name="last_ip" id="last_id"/></td>
  13.           </tr>
  14.           <tr>
  15.             <td align="right" style="width:125px ">VLAN Subnet Mask :</td>
  16.             <td align="left"><input type="text" style="width:100px " name="subnet_mask"/></td>
  17.           </tr>
  18.         </table>
Thanks and regards,
Vikas
Jun 9 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Notice on line 12, you have:
Expand|Select|Wrap|Line Numbers
  1. <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.
Jun 9 '08 #9

vikas251074
100+
P: 198
Thanks for find such an error. Still it is not assigning value to 'last_ip' field.

Now I changed to code like this
Expand|Select|Wrap|Line Numbers
  1. function firstip(temp){
  2.   var str=temp.value;
  3.   var i=0;
  4.   var dot= new Array();
  5.   dot=str.split('.');
  6.   for(i=0;i<=dot.length-1;i++){
  7.     if(dot[i]<=0 || dot[i]>=255){
  8.       alert('IP should be in the range 0 to 255');
  9.     }
  10.   }
  11.   var str1 = str.substring(0, lastIndexOf('.'));
  12.   document.getElementById('last_ip').value = str1 + '.' + 255;
  13. }
And calling portion of HTML script are -
Expand|Select|Wrap|Line Numbers
  1.     <table>
  2.  <tr>
  3.     <td align="right" style="width:125px ">VLAN Name :</td>
  4.     <td align="left"><input type="text" style="width:250px " name="vlan_name" id="vlan_name" onkeyup="capital(this);"/></td>
  5.   </tr>
  6.   <tr>
  7.     <td align="right" style="width:125px ">VLAN First IP :</td>
  8.     <td align="left"><input type="text" style="width:100px " name="first_ip" id="first_ip" onchange="firstip(this);"/></td>
  9.   </tr>
  10.   <tr>
  11.     <td align="right" style="width:125px ">VLAN Last IP :</td>
  12.     <td align="left"><input type="text" style="width:100px " name="last_ip" id="last_ip"/></td>
  13.   </tr>
  14.   <tr>
  15.     <td align="right" style="width:125px ">VLAN Subnet Mask :</td>
  16.     <td align="left"><input type="text" style="width:100px " name="subnet_mask"/></td>
  17.   </tr>
  18. </table>
Thanks and regards,
Vikas
Jun 10 '08 #10

vikas251074
100+
P: 198
Yes sir, I got it. Thanks everybody for help.
Error was in line no. 11 of function description.
Expand|Select|Wrap|Line Numbers
  1. var str1 = str.substring(0, lastIndexOf('.'));
silly mistake was 'str' was not added to lastIndexOf('.')

Thanks and regards
Vikas
Jun 10 '08 #11

acoder
Expert Mod 15k+
P: 16,027
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.
Jun 10 '08 #12

vikas251074
100+
P: 198
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
Jun 13 '08 #13

acoder
Expert Mod 15k+
P: 16,027
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.
Jun 13 '08 #14

vikas251074
100+
P: 198
But sir I have to verify IP address without submitting form from oracle database, so how can I use ASP.
Jun 13 '08 #15

acoder
Expert Mod 15k+
P: 16,027
Using Ajax, of course as I'm sure you've done already.
Jun 13 '08 #16

vikas251074
100+
P: 198
I have used this AJAX but to display the second list value as soon as the first list value is selected. I know how to verify the ip_address with oracle database, but don't know how to display message without loosing screen formats. Please give me some code for this for verify data and display message. Please. please. please ....

Thanks and regards,
Vikas
Jun 13 '08 #17

acoder
Expert Mod 15k+
P: 16,027
Where do you want to display it? Show some code.

The basic idea is that you only output the message from the ASP script and set the responseText of the Ajax object to a div on the main page.
Jun 13 '08 #18

vikas251074
100+
P: 198
Here is the code. When I select IP address, it should display description & mac_address from database without submitting form.

Expand|Select|Wrap|Line Numbers
  1.           <tr>
  2. <%          set rs=conn.execute("select ip_address from vlan_detail where item ='"&v_item&"' and vlan_name='"&v_lan&"' and seq_no = (select min(seq_no) from vlan_detail where allotted_to is null and item ='"&v_item&"' and vlan_name='"&v_lan&"')")
  3.             dim v_ip_address
  4.             v_ip_address = rs("ip_address") %>
  5.             <td align="right">IP Address : </td>
  6.             <td align="left">
  7.               <select name="ip_address" style="width:150px " >
  8. <%              set rs=conn.execute("select ip_address from vlan_detail where item ='"&v_item&"' and vlan_name='"&v_lan&"'")
  9.                 do while not rs.eof
  10.                   if rs("ip_address") = v_ip_address then%>
  11.                     <option value="<%=rs("ip_address")%>" selected><%=rs("ip_address")%></option>
  12. <%                  else %>
  13.                     <option value="<%=rs("ip_address")%>"><%=rs("ip_address")%></option>
  14. <%                end if 
  15.                   rs.movenext
  16.                 loop  %>
  17.               </select>
  18.             </td>
  19.           </tr>
  20.           <tr>
  21.             <td align="right">Description : </td>
  22.             <td align="left"><input type="text" name="emp_name" style="width:300px "/></td>
  23.           </tr>
  24.           <tr>
  25.             <td align="right">MAC Address : </td>
  26.             <td align="left"><input type="text" name="mac_addr" style="width:250px "/></td>
  27.           </tr>
  28.         </table>
  29.       </td>
  30.     </tr>
Thanks and regards,
Vikas
Jun 14 '08 #19

acoder
Expert Mod 15k+
P: 16,027
In your ASP script, return the description and MAC address separated by a delimiter, e.g. "||". Then use split() on the response to split it:
Expand|Select|Wrap|Line Numbers
  1. var resp = ajaxobj.responseText.split("||");
  2. desc = resp[0]; // contains description
  3. mac_addr = resp[1]; // contains mac address
  4.  
Jun 14 '08 #20

Post your reply

Sign in to post your reply or Sign up for a free account.