• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IP address validation using javascript

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does any body has an example or function to do above validation?
Thanks,
Angela
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at this
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic