0

I have a form and JQuery validation. Everything works fine. Instead of showing messages, I want to show icons.

The wrong icon is working, but I have some problem with the right icon. When input is a right format, I see class="valid" in the syntax <input class="valid"...>. I try to display a right icon after <input..> by using class="valid". The right icon shows right on the text field and the text field disappears. LIVE CODE

Please help.

HTML

<form method="POST" id="TestForm">
    <input name="txtIn" type="text" id="txtIn">
    <p></p>
    <button id="submit">Submit</button>
</form>

CSS

label.error:after {
    content:"";
    display: inline-block;
    margin-left:10px;
    background: url("http://dummyimage.com/15x15/de1417/de1417.png") no-repeat;
    width: 15px;
    height: 15px;
}

.valid{
    margin-left:10px;
    background: green;
    width: 15px;
    height: 15px;
}

JS

$('#TestForm').validate({
    rules: {
        txtIn: {
            required: true,
            rangelength: [8, 16]
        }
    },
    messages: {
        txtIn: {
            required: "Please enter something",
            rangelength: "Icon"
        }




    }
});
$('.valid').after('<div class="valid"></div>');
abcid d
  • 2,821
  • 7
  • 31
  • 46
  • your `after()` is only going to be called on page load and the valid class doesn't exist then – charlietfl Oct 12 '14 at 14:28
  • @ Charlie, Thank you! but this this case, what I have to use instead of `after()`? – abcid d Oct 12 '14 at 17:17
  • can use `highlight` and `unhighlight` options to do your own manipulation – charlietfl Oct 12 '14 at 17:22
  • Just updated my answer, possibly with a working solution for the valid icon. – matthias_h Oct 12 '14 at 19:56
  • @abcidd To avoid extended comments below the answer - I've just updated your Fiddle, hope this will work now as intended. – matthias_h Oct 12 '14 at 23:54
  • @abcidd Added a new approach in updated answer. – matthias_h Oct 13 '14 at 21:06
  • @ Matthias, Sorry I was out for 2 days. Update 4 is a nice approach! Thank you so much! You wrote a great code on focusInvalid: false, focusCleanup: true, and onkeyup. They work so smoothly! Amazing... However, I didn't get ` onfocusout: function (element) { this.element(element); },` Please explain. Thank you so much! – abcid d Oct 16 '14 at 01:03

1 Answers1

0

Unfortunately, the validation in the Fiddle is not always working for correct validation, but you can try to adjust $('.valid').after('<div class="valid"></div>'); to $('.valid').after('<div class="validicon"></div>'); and rename your class valid to validicon. Otherwise the input that will be set to class valid will be applied the css with width 15px etc and therefore not be visible.

Update: For the follow-up issues mentioned in the comments: You can try to add display:inline-block; to class erroricon. And as kind of second workaround - if it's ok for you to display kind of an erroricon inside the input instead of next to it, an easy way would be this:

input.valid {
   background-image: url('http://dummyimage.com/15x15/00ff11/fff.png&text=+');
   background-position: top right;
   background-size: 15px 15px;
   background-repeat: no-repeat;
}

Update 2: I've just checked the options for the validate plugin. It's possible to add a label for valid elements by extending your settings for validate() like this:

$("#TestForm'").validate({
   // keep your other settings/options for rules
   // and messages here and add:
  success: function(label) {  
   label.addClass("valid");
  }
});  

As mentioned in the comments, it's only possible to use css pseudo elements like :after or :before for container elements, not for elements like input or image. Good explanation for this provided at this comment by Robert Koritnik: Can I use the :after pseudo-element on an input field?. In case it's possible to add a label with approach suggested above, you can add css for label.valid:after {... in the same way as for label.error:after.

Update 3: For the next approach mentioned in the comments by OP - as adding the label with class valid doesn't seem to work - I just adjusted the Fiddle.
HTML adjustment - I moved the <span class="add"></span> after the <input> instead of wrapping the input in the span.
jquery adjustment:

if ($('#TestForm input').hasClass('valid')) {
  $('.add').addClass('validicon');
 } else {
  $('.add').removeClass('validicon');
}

As mentioned, if the input is valid, the Fiddle throws a network error 403 CSRF verification failed, but it should work on the actual site.

Update 4: New approach using the options of jquery validate instead of trying a workaround. Adjusted Fiddle to display valid icon on submit without sending the form.

HTML:

<form id="TestForm">
 <input type="text" id="txtIn" name="txtIn" />
 <p></p>
 <button id="submit">Submit</button>
</form>

CSS:

label {
 display: inline-block;
 margin-left:10px;
 width: 15px;
 height: 15px;
}
label.error {
 background-color: red;
}
label.valid {
 background-color: blue;
}

jquery:

$(document).ready(function () {

  var messages = {
    'txtInRequired': ""
  };

  $('#TestForm').validate({
    rules: {
        txtIn: {
            required: true,
            rangelength: [8, 16]
        }
    },
    messages: {
        txtIn: messages.txtInRequired
    },
    onfocusout: function (element) {
        this.element(element);
    },
    success: function (label, element) {
        if ($(element).hasClass("valid")) {
            label.addClass("valid");
        }
    },
    submitHandler: function (form) {
        alert('form is valid');
        return false;
    },
    focusInvalid: false,
    focusCleanup: true,
    onkeyup: false
  });
});

For reference for these adjustments: http://jqueryvalidation.org/category/plugin/

Short explanation:
Setting focusInvalid: false: The input won't get focus when the form was submitted and the input is invalid, necessary to combine with focusCleanup: true : when the input gets focus, validation is "cleaned", so the error or icon label won't be displayed. onkeyup: false : on enter the input is not validated. And finally: the success: function (label, element) { .. } adds class valid to the label in case the element is valid.

Reference: http://jqueryvalidation.org/validate/

Community
  • 1
  • 1
matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • @ Matthias, Thank you very much again! I try your direction, but it doesn't work out. I also try `$('input .valid').addClass('validicon').after('
    ');` it doesn't work, either.
    – abcid d Oct 12 '14 at 17:03
  • @abcidd I've just adjusted my answer, $('.valid').after('
    '); could work.
    – matthias_h Oct 12 '14 at 17:15
  • @ Matthias, Thank you! I try, but it doesn't work out. – abcid d Oct 12 '14 at 17:22
  • @abcidd Can you check if the div "validicon" is added after the input? – matthias_h Oct 12 '14 at 17:26
  • It doesn't show `.validicon` when the format is right. If I use `$('.valid').after('
    ');` the icon shows, but it shows right on the text field and the text field disappears.
    – abcid d Oct 12 '14 at 17:40
  • @abcidd Updated my answer with a guess and another workaround. Fiddle is just not working for correct validation, than it would be easier to check what's going on. – matthias_h Oct 12 '14 at 17:51
  • @ Matthias, I have tried this: I don't need to have `$('.valid').after('
    ');` and in CSS I added input.valid:after{ content:""; display: inline-block; margin-left:10px; background-color: blue; width: 15px; height: 15px; } When I inspected HTML, I saw `input.valid:after` there, but it doesn't show on the browser!!! I wonder why???
    – abcid d Oct 12 '14 at 19:31
  • @abcidd It doesn't show in the browser because pseudo-elements like :before and :after only work for container elements, not for e.g. inputs (also not for images). For detail: http://stackoverflow.com/questions/2587669/css-after-pseudo-element-on-input-field#comment-5133582 – matthias_h Oct 12 '14 at 19:41
  • Thank you very much, Matthias! so that it is not a right way to approach. – abcid d Oct 12 '14 at 20:40
  • @ Matthias, You are right! pseudo-element doesn't work on ``. I also try `success: function(label) {..` as your direction, it doesn't work, either because when input is a right format, the ` – abcid d Oct 12 '14 at 23:30
  • @ Matthias, Good News, after ``I add `` and in CSS, I write `input.valid + i.validicon{....` it works [LIVE CODE](http://jsfiddle.net/6y2rwvae/17/). However, when I try to enter a wrong format and hit tab, it shows the error, then I go back to edit to a right format and hit tab, the right icon doesn't show! this is because `` is already in the code. Do you know where to delete this error label in the jquery.validate.js? I try to find it out, but I have no clue! – abcid d Oct 13 '14 at 00:25