all
alt
ch
dir
id
min
rel
top
url
URL
urn
Expand Minimize

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.

HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 4

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 a Windows Store app using JavaScript 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
HTMLSelectElement

 

 

Show:
© 2014 Microsoft