Hopefully a quick question. I’m trying to validate a double. Making sure it’s positive to one decimal place.
Good Values: 0.1, 2.5, 100.1, 1, 54
Bad Values: -0.1, 1.123, 1.12, abc, 00012.1
So I’ve tried Regex here:
public boolean isValidAge(String doubleNumber)
{
DoubleValidator doubleValidator = new DoubleValidator();
return doubleValidator.isValid(doubleNumber,"*[0-9]\\\\.[0-9]");
}
I’ve also tried: "*[0-9].[0-9]", "\\\\d+(\\\\.\\\\d{1})?", "[0-9]+(\\\\.[0-9]?)?"
Nothing seems to be working. I’ve been using org.apache.commons.validator.routines.DoubleValidator
Is there another way I can do this any reason why these regex’s aren’t working? I don’t have to use regex.
Thanks
This will match a number and only a number with either a multi-digit pre-decimal point number with no leading zero(s), or just a zero, and optionally a decimal point with one decimal digit. Includes match of the beginning/end of string, so it won’t match a number in a larger string (updated to not accept leading zeros, credit @Nicklas A):
Use the
java.util.regex.Patternlibrary instead.http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
Because you’re putting the regex into a string, be sure to escape the backslashes
I recommend that you read up on regular expressions here, they are an important tool to have “under your belt” so to speak.
http://www.regular-expressions.info/