Validate input manually in Materialize

Two easy functions to set any input-form or textarea as wrong or success with custom messages.


Libraries

Code Snippet
function setTextInputWrong(field, message) {
    var label = document.querySelectorAll("label[for=" + $(field).attr("id") + "]");
    if (label != null)
        $(label).attr("data-error", message);
    $(field).addClass("invalid");
}

function setTextInputSuccess(field, message) {
    var label = document.querySelectorAll("label[for=" + $(field).attr("id") + "]");
    if (label != null)
        $(label).attr("data-success", message);
    $(field).addClass("valid");
}


Implementation

Use for exampe <code>setTextInputSuccess($("#myInput"), "You did everything right! ;)");</code> Note that your code should look like the following <code><input id="myInput" type="text"> <label for="myInput" class="active">Title</label></code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Materialize



    Informations

    posted on 2016-10-10 18:50:52

    viewed 134 times

    snippet's UUID is 57fbc6ec2407d

    Similar Snippets