Search code examples
javascriptjqueryhtmlsmoke

How to add custom rules in SMOKE js validation plugin?


I don't know how to add custom rules in SMOKE validation plugin. For example I want to add validate username. I know that how to validate username. But, I want to know that how to add that rules in that plugin. this is the code I've written

$('#login-submit').click(function (e) {
        e.preventDefault();
        if ($('#login-form').smkValidate()) {
            $.smkAlert({
                text: 'You logged in successfully!',
                type: 'success'
            });
        }
    });

and this is the function created to validate username. it returns false if username is not valid.

function isUsernameValid(username) {
    return /^[0-9a-zA-Z_.-]+$/.test(username);
 }

so how can i insert a custom rule to this script if i want to. Thank you!


Solution

  • you should use data-smk-pattern attribute for the username input

    in your case:

    <input type="text" class="form-control" data-smk-pattern="^[0-9a-zA-Z_.-]+$">

    you can also refer to the smoke documentation: http://alfredobarron.github.io/smoke/#/validate#pattern