How to show validation message below each textbox using jquery?

后端 未结 5 1760
野的像风
野的像风 2020-12-06 18:45

I am trying to make a login form in which I have email address and password as the textbox. I have done the validation on the email address part so that it should have prope

相关标签:
5条回答
  • 2020-12-06 18:50

    This is the simple solution may work for you.

    Check this solution

    $('form').on('submit', function (e) {
    e.preventDefault();
    var emailBox=$("#email");
    var passBox=$("#password");
    if (!emailBox.val() || !passBox.val()) {
        $(".validationText").text("Please Enter Value").show();
    }
    else if(!IsEmail(emailBox.val()))
    {
        emailBox.prev().text("Invalid E-mail").show();
    }
    $("input#email, input#password").focus(function(){
        $(this).prev(".validationText").hide();
    });});
    
    0 讨论(0)
  • 2020-12-06 19:02

    Here you go:

    JS:

    $('form').on('submit', function (e) {
        e.preventDefault();
    
        if (!$('#email').val()) 
            $('#email').parent().append('<span class="error">Please enter your email address.</span>');
    
    
        if(!$('#password').val())
             $('#password').parent().append('<span class="error">Please enter your password.</span>');
    });
    

    CSS:

    @charset "utf-8";
    /* CSS Document */
    
    /* ---------- FONTAWESOME ---------- */
    /* ---------- http://fortawesome.github.com/Font-Awesome/ ---------- */
    /* ---------- http://weloveiconfonts.com/ ---------- */
    
    @import url(http://weloveiconfonts.com/api/?family=fontawesome);
    
    /* ---------- ERIC MEYER'S RESET CSS ---------- */
    /* ---------- http://meyerweb.com/eric/tools/css/reset/ ---------- */
    
    @import url(http://meyerweb.com/eric/tools/css/reset/reset.css);
    
    /* ---------- FONTAWESOME ---------- */
    
    [class*="fontawesome-"]:before {
      font-family: 'FontAwesome', sans-serif;
    }
    
    /* ---------- GENERAL ---------- */
    
    body {
        background-color: #C0C0C0;
        color: #000;
        font-family: "Varela Round", Arial, Helvetica, sans-serif;
        font-size: 16px;
        line-height: 1.5em;
    }
    
    input {
        border: none;
        font-family: inherit;
        font-size: inherit;
        font-weight: inherit;
        line-height: inherit;
        -webkit-appearance: none;
    }
    
    /* ---------- LOGIN ---------- */
    
    #login {
        margin: 50px auto;
        width: 400px;
    }
    
    #login h2 {
        background-color: #f95252;
        -webkit-border-radius: 20px 20px 0 0;
        -moz-border-radius: 20px 20px 0 0;
        border-radius: 20px 20px 0 0;
        color: #fff;
        font-size: 28px;
        padding: 20px 26px;
    }
    
    #login h2 span[class*="fontawesome-"] {
        margin-right: 14px;
    }
    
    #login fieldset {
        background-color: #fff;
        -webkit-border-radius: 0 0 20px 20px;
        -moz-border-radius: 0 0 20px 20px;
        border-radius: 0 0 20px 20px;
        padding: 20px 26px;
    }
    
    #login fieldset div {
        color: #777;
        margin-bottom: 14px;
    }
    
    #login fieldset p:last-child {
        margin-bottom: 0;
    }
    
    #login fieldset input {
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        border-radius: 3px;
    }
    
    #login fieldset .error {
        display: block;
         color: #FF1000;
        font-size: 12px;
    }
    }
    
    #login fieldset input[type="email"], #login fieldset input[type="password"] {
        background-color: #eee;
        color: #777;
        padding: 4px 10px;
        width: 328px;
    }
    
    #login fieldset input[type="submit"] {
        background-color: #33cc77;
        color: #fff;
        display: block;
        margin: 0 auto;
        padding: 4px 0;
        width: 100px;
    }
    
    #login fieldset input[type="submit"]:hover {
        background-color: #28ad63;
    }
    

    HTML:

    <div id="login">
    
    <h2><span class="fontawesome-lock"></span>Sign In</h2>
    
    <form action="javascript:void(0);" method="POST">
    
        <fieldset>
    
            <div><label for="email">E-mail address</label></div>
            <div><input type="email" id="email" /></div>
    
            <div><label for="password">Password</label></div>
            <div><input type="password" id="password" /></div> <!-- JS because of IE support; better: placeholder="Email" -->
    
            <div><input type="submit" value="Sign In"></div>
    
        </fieldset>
    
    </form>
    

    And the fiddle: jsfiddle

    0 讨论(0)
  • 2020-12-06 19:03

    The way I would do it is to create paragraph tags where you want your error messages with the same class and show them when the data is invalid. Here is my fiddle

    if ($('#email').val() == '' || !$('#password').val() == '') {
        $('.loginError').show();
        return false;
    }
    

    I also added the paragraph tags below the email and password inputs

    <p class="loginError" style="display:none;">please enter your email address or password.</p>
    
    0 讨论(0)
  • 2020-12-06 19:13

    You could put static elements after the fields and show them, or you could inject the validation message dynamically. See the below example for how to inject dynamically.

    This example also follows the best practice of setting focus to the blank field so user can easily correct the issue.

    Note that you could easily genericize this to work with any label & field (for required fields anyway), instead of my example which specifically codes each validation.

    Your fiddle is updated, see here: jsfiddle

    The code:

    $('form').on('submit', function (e) {
        var focusSet = false;
        if (!$('#email').val()) {
            if ($("#email").parent().next(".validation").length == 0) // only add if not added
            {
                $("#email").parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please enter email address</div>");
            }
            e.preventDefault(); // prevent form from POST to server
            $('#email').focus();
            focusSet = true;
        } else {
            $("#email").parent().next(".validation").remove(); // remove it
        }
        if (!$('#password').val()) {
            if ($("#password").parent().next(".validation").length == 0) // only add if not added
            {
                $("#password").parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please enter password</div>");
            }
            e.preventDefault(); // prevent form from POST to server
            if (!focusSet) {
                $("#password").focus();
            }
        } else {
            $("#password").parent().next(".validation").remove(); // remove it
        }
    });  
    

    The CSS:

        .validation
        {
          color: red;
          margin-bottom: 20px;
        }
    
    0 讨论(0)
  • 2020-12-06 19:13

    is only the matter of finding the dom where you want to insert the the text.

    DEMO jsfiddle

    $().text(); 
    
    0 讨论(0)
提交回复
热议问题