• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IP address validation using javascript

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one text box in html. I want to do some validation so user can enter in following format only:
(0-255).(0-255).(0-255).(0.255)
User can enter number only between 0-255.
How can I do this?
Thanks,
Angela
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point do you want to do the validation? You can monitor the field with onchange handlers as they are entered, or you can validate the entries when the form is to be subimtted with an onsubmit handler on the form.
I'd recommend the latter.
hth,
bear
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does any body has an example or function to do above validation?
Thanks,
Angela
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at this
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It gives Valid IP address alert message for following values 123123123123 using this following function:
<script>
var IPText = "123.123.123.123";
RegE = /^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$/
if(IPText.match(RegE))alert('Valid IP');
else alert('Invalid IP');
</script>
Isn't should give Invalid IP address aler message for 123123123(which without decimal)
Thanks,
Angela
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you want it valid with or without the "." ?
If so:
RegE = /^\d{1,3}.{0,1}\d{1,3}.{0,1}\d{1,3}.{0,1}\d{1,3}$/
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:
so you want it valid with or without the "." ?
If so:
RegE = /^\d{1,3}.{0,1}\d{1,3}.{0,1}\d{1,3}.{0,1}\d{1,3}$/


I want with ".".
I tried above but still not working. When I tried value 123123123123. It gives Valid IP address. It should give Invalid IP address message.
Thanks,
Angela
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ugh my mistake
RegE = /^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$/
okay now that that works but you will run into a problem with that...opps...hehehehe
It will allow numbers greater then 255 so, i need to change that
I will post a solution in a minut or two
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here I got away from reg. exp. and did a quick string manipulation thing...
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great!jumpingjoy: It's working. Thanks a lot for your help.

Angela :
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Simnan Ali
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:here I got away from reg. exp. and did a quick string manipulation thing...







this script pops true/false but i want to pop Valid /invalid ip...wil you suggest me a way to do it??
 
Nasir Iqball
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:here I got away from reg. exp. and did a quick string manipulation thing...



Add an isNaN on ipParts[i] to make it accurate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic