aspose file tools*
The moose likes Java in General and the fly likes How to validate Text field in java.It should accept only numric value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to validate Text field in java.It should accept only numric value." Watch "How to validate Text field in java.It should accept only numric value." New topic
Author

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

Ravi Sinha
Greenhorn

Joined: Nov 04, 2003
Posts: 13
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

Joined: Sep 22, 2001
Posts: 2578

Have a look at JFormattedTextField, it does have some nice exmplaes of validation formats as well.

HTH,
- Manish
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
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

Joined: Dec 17, 2004
Posts: 68
FileName: CheckDigits.java


Hope This Helps.
All The Best
 
wood burning stoves
 
subject: How to validate Text field in java.It should accept only numric value.