w--
w--

Reputation: 6667

django - custom error message for urlinput widget

django 1.7 trying to customize the default client side validation message that gets fired on a model form. Specifically the error message for urlinput widget (the default for a urlfield). This error appears to come from client side validation when the form is submitted.
The target error message

I want to change "Please enter a URL" to something else. I feel like I've looked everywhere but can't find it.

Can anyone help point me in the right direction?

Upvotes: 0

Views: 646

Answers (1)

Raja Simon
Raja Simon

Reputation: 10305

The url type in Html5 will display required attribute message lilke this

Cross browser verbiage:
    Firefox: Please enter a URL.
    Chrome: Please enter a URL.
    IE: You must enter a valid URL

You can use setcustomvalidity

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
    var elements = document.getElementsByTagName("INPUT");
    for (var i = 0; i < elements.length; i++) {
        elements[i].oninvalid = function(e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("This field cannot be left blank");
            }
        };
        elements[i].oninput = function(e) {
            e.target.setCustomValidity("");
        };
    }
})
</script>

Upvotes: 1

Related Questions