Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Sinha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at JFormattedTextField, it does have some nice exmplaes of validation formats as well.

HTH,
- Manish
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • 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++);
}

}
}
}
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileName: CheckDigits.java


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