• Post Reply Bookmark Topic Watch Topic
  • New Topic

IP address validation in Text Filed

 
Nagarajan Lakshamanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me..i m helpless in my project..i want to know how to validate the text field with IP address as an input(xx.xx.xx.xx)IPv4....with the predefined dot operator..like the input we give in TCP/IPV4 network properties(windows)..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class has various operations that would be helpful with this, including indexOf and substring. You may also wish to ensure that the numbers are all between 0 and 255; Integer.parseInt can help with that.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

You may be able to create a regular expression, or even Google for one ready-made.
 
Nagarajan Lakshamanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please post the code for it..it will be helpful..for me,i ve tried with the JFormattedTextField...but it does't work 4 me...
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are NotACodeMill. We will help you find a solution but not provide full solutions like that.

You have two options:
1) create a java.text.Format subclass for IPv4 addresses. That shouldn't be too hard actually, all you need to override is the format(Object, StringBuffer, FieldPosition) and parseObject(String, ParsePosition) methods. Preferably you also create methods with similar arguments as the parseObject methods but that return something more useful than Object. Something like DateFormat's parse(...) methods. Of course, you'll let parseObject(...) return the results of parse(...).
2) create a javax.swing.JFormattedTextField.AbstractFormatter subclass. Again two methods to override: stringToValue and valueToString.

I'd go for option 1, since that class will be a bit more useful, with a broader use base (not just JFormattedTextField). You can easily turn your Format into an AbstractFormatter using javax.swing.text.InternationalFormatter; vice versa is not that easy.
 
Nagarajan Lakshamanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k thnks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!