How to validate Text field in java.It should accept only numric value.
Ravi Sinha
Greenhorn
Joined: Nov 04, 2003
Posts: 13
posted
0
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.
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++); }