I am trying to check if the height is valid. To be valid it must be 1 or 1.4 or 1.0 or 1. etc it cannot be anything else like a string, any chars beside a number or a decimal. The decimal must not be at the start of the string and it can only have 1 decimal. The regex I have been trying is:
"/^[0-9]+([\\,\\.][0-9]+)?$/"
But it is not working and still accepts strings. Any help would be great, thanks!
public static boolean HeightValid(String height)
{
boolean isHeightValid = true;
if(!height.matches("/^[0-9]+([\\,\\.][0-9]+)?$/") || height.length() < 1 || height.length() > 9)
{
isHeightValid = false;
}
else
{
double doubleHeight = Double.parseDouble(height);
DecimalFormat df = new DecimalFormat("#.00");
String decHeight = df.format(doubleHeight);
heightField.setText(decHeight);
}
return isHeightValid;
}
If you need any more information just add a comment, thank you