如何在登录验证中自定义验证消息位置?

时间:2016-02-15 08:15:57

标签: javascript html validation jquery-ui tooltip

 <input type="text" class="form-control" name="username"  placeholder="Enter ID " required oninvalid="this.setCustomValidity('Enter ID')" oninput="setCustomValidity('')" />

如何自定义文本框右侧的验证邮件位置?现在它位于文本框下方。

Plunker链接:http://plnkr.co/edit/vvfR5pelzeJAMM5LagC9?p=preview

2 个答案:

答案 0 :(得分:0)

使用父元素的元素的位置使用相对,并使用工具提示的绝对位置。如果可能,请显示您的演示代码

答案 1 :(得分:0)

以下是jQuery依赖脚本的示例。              

    <div>
        <label for="name">Name:</label>
        <input id="name" type="text" required>
    </div>

    <div>
        <label for="comments">Comments:</label>
        <textarea id="comments" required></textarea>
    </div>

    <div class="buttons">
        <button>Submit</button>
    </div>
</form>​

<script>
    var createAllErrors = function() {
        var form = $( this ),
            errorList = $( "ul.errorMessages", form );

        var showAllErrorMessages = function() {
            errorList.empty();

            // Find all invalid fields within the form.
            var invalidFields = form.find( ":invalid" ).each( function( index, node ) {

                // Find the field's corresponding label
                var label = $( "label[for=" + node.id + "] "),
                    // Opera incorrectly does not fill the validationMessage property.
                    message = node.validationMessage || 'Invalid value.';

                errorList
                    .show()
                    .append( "<li><span>" + label.html() + "</span> " + message + "</li>" );
            });
        };

        // Support Safari
        form.on( "submit", function( event ) {
            if ( this.checkValidity && !this.checkValidity() ) {
                $( this ).find( ":invalid" ).first().focus();
                event.preventDefault();
            }
        });

        $( "input[type=submit], button:not([type=button])", form )
            .on( "click", showAllErrorMessages);

        $( "input", form ).on( "keypress", function( event ) {
            var type = $( this ).attr( "type" );
            if ( /date|email|month|number|search|tel|text|time|url|week/.test ( type )
              && event.keyCode == 13 ) {
                showAllErrorMessages();
            }
        });
    };

    $( "form" ).each( createAllErrors );
</script>