View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.html;
16  
17  import org.htmlunit.html.DomNode;
18  import org.htmlunit.html.ValidatableElement;
19  import org.htmlunit.javascript.HtmlUnitScriptable;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  
24  /**
25   * A JavaScript object for {@code ValidityState}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   *
30   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/ValidityState">MDN Documentation</a>
31   */
32  @JsxClass
33  public class ValidityState extends HtmlUnitScriptable {
34  
35      /**
36       * JavaScript constructor.
37       */
38      @JsxConstructor
39      public void jsConstructor() {
40          // nothing to do
41      }
42  
43      private ValidatableElement getValidatableElementOrDie() {
44          return (ValidatableElement) getDomNodeOrDie();
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public void setDomNode(final DomNode domNode) {
52          setDomNode(domNode, false);
53      }
54  
55      /**
56       * Returns a boolean value that is true if the user has provided
57       * input that the browser is unable to convert.
58       * @return a boolean value that is true if the user has provided
59       *         input that the browser is unable to convert.
60       */
61      @JsxGetter
62      public boolean isBadInput() {
63          return getValidatableElementOrDie().hasBadInputValidityState();
64      }
65  
66      /**
67       * Returns whether the custom validity message is set.
68       * @return whether the custom validity message is set
69       */
70      @JsxGetter
71      public boolean isCustomError() {
72          return getValidatableElementOrDie().isCustomErrorValidityState();
73      }
74  
75      /**
76       * Returns whether the element value does not match its {@code pattern} attribute.
77       * @return whether the element value does not match its {@code pattern} attribute
78       */
79      @JsxGetter
80      public boolean isPatternMismatch() {
81          return getValidatableElementOrDie().hasPatternMismatchValidityState();
82      }
83  
84      /**
85       * Returns whether the element value is greater than its {@code max} attribute.
86       * @return whether the element value is greater than its {@code max} attribute
87       */
88      @JsxGetter
89      public boolean isRangeOverflow() {
90          return getValidatableElementOrDie().hasRangeOverflowValidityState();
91      }
92  
93      /**
94       * Returns whether the element value is less than its {@code min} attribute.
95       * @return whether the element value is less than its {@code min} attribute
96       */
97      @JsxGetter
98      public boolean isRangeUnderflow() {
99          return getValidatableElementOrDie().hasRangeUnderflowValidityState();
100     }
101 
102     /**
103      * Returns whether the element value is invalid per its {@code step} attribute.
104      * @return whether the element value is invalid per its {@code step} attribute
105      */
106     @JsxGetter
107     public boolean isStepMismatch() {
108         return getValidatableElementOrDie().isStepMismatchValidityState();
109     }
110 
111     /**
112      * Returns whether the element value exceeds its {@code maxLength} attribute.
113      * @return whether the element value exceeds its {@code maxLength} attribute
114      */
115     @JsxGetter
116     public boolean isTooLong() {
117         return getValidatableElementOrDie().isTooLongValidityState();
118     }
119 
120     /**
121      * Returns whether the element value exceeds its {@code minLength} attribute.
122      * @return whether the element value exceeds its {@code minLength} attribute
123      */
124     @JsxGetter
125     public boolean isTooShort() {
126         return getValidatableElementOrDie().isTooShortValidityState();
127     }
128 
129     /**
130      * Returns whether the element value is invalid per its {@code type} attribute.
131      * @return whether the element value is invalid per its {@code type} attribute
132      */
133     @JsxGetter
134     public boolean isTypeMismatch() {
135         return getValidatableElementOrDie().hasTypeMismatchValidityState();
136     }
137 
138     /**
139      * Returns whether the element (with a {@code required} attribute) has no value.
140      * @return whether the element (with a {@code required} attribute) has no value
141      */
142     @JsxGetter
143     public boolean isValueMissing() {
144         return getValidatableElementOrDie().isValueMissingValidityState();
145     }
146 
147     /**
148      * Returns whether the element value is valid.
149      * @return whether the element value is valid
150      */
151     @JsxGetter
152     public boolean isValid() {
153         return getValidatableElementOrDie().isValidValidityState();
154     }
155 }