• 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

How to validate Text field in java.It should accept only numric value.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There!
I shall be thankfull if u will help me out writing method where it should validate text field(in java).It should accept only numric value.
These are following criteria:
The number is between 1 and 12 digits in length
The number is all digits (i.e. contains no alpha characters)
The number is not all zeros
If the number is 7 or more digits in length then
--The number cannot be all the same digit
--The number cannot be a sequence where zero is in sequence with both 1 and 9.

waiting for reply.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at JFormattedTextField, it does have some nice exmplaes of validation formats as well.

HTH,
- Manish
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Here is an example for that.

public void test()
{
TextField t = new TextField(29);

String s="134a897";
if((s.length() > 0) && (s.length() < 13))
{
System.out.println("inside first loop");

for(int i=0;i<s.length();i++) // check for all digits
{
switch(s.charAt(i))
{
case '0' : break;
case '1' : break;
case '2' : break;
case '3' : break;
case '4' : break;
case '5' : break;
case '6' : break;
case '7' : break;
case '8' : break;
case '9' : break;
default : throw new RuntimeException("Not all digits");
}

}
if(s.length() >= 7) // check for total digits more than 7
{
int i=0;
for(;s.charAt(i) == s.charAt(i+1);i++) // check for all digits same
{
if(i >= (s.length()-2))
break;
}
System.out.println("outside i "+i+" "+s.length());
if(i==s.length()-2){
System.out.println("inside i "+i+" "+s.length());
throw new RuntimeException("Number with same digits"); }

int c=0;
i=s.indexOf('1',c);

while((i != -1) && (c <=6) )
{
System.out.println("inside while "+i+ "c "+c);
if(s.charAt(c+1)=='0')
{
throw new RuntimeException("Number with sequence as 10");
}
i=s.indexOf('1',c++);
}

i=c=0;
i=s.indexOf('9',c);
while((i != -1) && (c <=6))
{
if(s.charAt(i+1)=='0')
{
throw new RuntimeException("Number with sequence as 90");
}
i=s.indexOf('9',c++);
}

}
}
}
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileName: CheckDigits.java


Hope This Helps.
All The Best
reply
    Bookmark Topic Watch Topic
  • New Topic