validationMessage property
Returns the error message that would be displayed if the user submits the form, or an empty string if no error message. It also triggers the standard error message, such as "this is a required field". The result is that the user sees validation messages without actually submitting.
This property is read-only.
![]() ![]() |
Syntax
| JavaScript |
|---|
sMessage = object.validationMessage |
Property values
Type: DOMString
Standards information
Remarks
Returns a string containing the standard or custom error message that would be displayed if the user submitted at this time. If no errors are present or the form would validate, an empty string is returned.
The following example has a required field, and if the user types "fun" into it, a custom message is set. Try previewing without anything in the field, then enter values, including the word fun:
Note For more code samples, see Form controls part 1 and Form controls part 2: validation on the Windows Internet Explorer sample site.
Examples
<form id="myForm"> <label>Type anything but "fun": <input id="noFun" type="text" oninput="checkValid(this)" required ><input type="submit"></label> <div><button onclick="previewMessage();">Preview errors</button></div> <div id="err"></div> </form> <script> function checkValid(input) { if (input.value == "fun") { input.setCustomValidity("You're having too much fun!"); } else { // input is fine -- reset the error message input.setCustomValidity(''); } } function previewMessage() { var myform = document.getElementById("noFun") document.getElementById("err").innerHTML = myform.validationMessage; } </script>
See also
- HTMLButtonElement
- HTMLFieldSetElement
- HTMLInputElement
- HTMLObjectElement
- HTMLOutputElement
- HTMLSelectElement
Show:

