Module org.htmlunit

Class HtmlNumberParser

java.lang.Object
org.htmlunit.html.parser.HtmlNumberParser

public final class HtmlNumberParser extends Object
Utility methods for parsing HTML floating-point numbers.

This parser implements the syntax defined by the HTML Standard for valid floating-point numbers, which is used by elements such as <input type="number">. The accepted syntax differs from both Java number literals and JavaScript numeric literals.

In particular:

  • only ASCII digits (0-9) are accepted,
  • hexadecimal, binary, and octal notation are not supported,
  • NaN and Infinity are rejected, and
  • leading or trailing whitespace is not permitted.
Author:
Ronald Brill
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isValid(String value, boolean acceptLeadingPlus, boolean acceptDotAtEnd)
    Returns whether the supplied string is a valid HTML floating-point number.
    static BigDecimal
    parse(String value, boolean acceptLeadingPlus, boolean acceptDotAtEnd)
    Parses the supplied string as an HTML floating-point number.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isValid

      public static boolean isValid(String value, boolean acceptLeadingPlus, boolean acceptDotAtEnd)
      Returns whether the supplied string is a valid HTML floating-point number.

      This method validates the complete input according to the HTML floating-point number grammar. A value is considered valid only if it can be parsed completely; partial matches are rejected.

      Parameters:
      value - the string to validate
      acceptLeadingPlus - set this to true to accept strings like "+7"
      acceptDotAtEnd - set this to true to accept strings like "1."
      Returns:
      true if the supplied string is a valid HTML floating-point number; false otherwise
      See Also:
    • parse

      public static BigDecimal parse(String value, boolean acceptLeadingPlus, boolean acceptDotAtEnd)
      Parses the supplied string as an HTML floating-point number.

      If the input is syntactically valid, its numeric value is returned as a BigDecimal. Otherwise null is returned.

      This method performs syntax validation only. It does not apply any additional constraints that may be imposed by individual HTML algorithms or form controls.

      Parameters:
      value - the string to parse
      acceptLeadingPlus - set this to true to accept strings like "+7"
      acceptDotAtEnd - set this to true to accept strings like "1."
      Returns:
      the parsed value, or null if the supplied string is not a valid HTML floating-point number