*
The moose likes HTML, CSS and JavaScript and the fly likes IP address validation using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "IP address validation using javascript" Watch "IP address validation using javascript" New topic
Author

IP address validation using javascript

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
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

Joined: Jan 10, 2002
Posts: 60782
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Does any body has an example or function to do above validation?
Thanks,
Angela
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
look at this
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 16, 2002
Posts: 469
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
here I got away from reg. exp. and did a quick string manipulation thing...
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
great!jumpingjoy: It's working. Thanks a lot for your help.

Angela :
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Simnan Ali
Greenhorn

Joined: Apr 25, 2013
Posts: 1
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

Joined: Sep 26, 2013
Posts: 1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IP address validation using javascript
 
Similar Threads
Default Enter key and submit BUTTON
Bypassing form validation
regular expression validation script
Validation of database path, port number and ip address
NX: some questions concerning find-method