Abdalla Arbab
Abdalla Arbab

Reputation: 1400

Use custom validation in Parsley js

I want to use Parsley js to allow only alphabetic, dashes and underscores. this is my code

<html>
    <head>
        <title>Alphadash</title>
        <link rel="stylesheet" href="css/Parsley.css">
    </head>
    <body>
        <form id="demo-form" data-parsley-validate="">
            <label>Please enter a alphadash:</label>
            <input type="text" name="slug" required data-parsley-alphadash="">

            <input type="submit">
        </form>

        <script src='js/jquery.min.js'></script>
        <script src='js/Parsley.min.js'></script>
        <script type="text/javascript">
        window.Parsley.addValidator('alphadash', {
          validateString: function(value) {
            return true == (/^\d+$/.test(value));
          },
          messages: {
            en: 'Only alphabetic letters, dashes and underscores allowed.'
          }
        });
        </script>
    </body>
</html>

I know you can do that with Pattern build-in function. but in my case i want to use a custom function.

Upvotes: 2

Views: 4882

Answers (2)

Marc-Andr&#233; Lafortune
Marc-Andr&#233; Lafortune

Reputation: 79612

Just use the builtin pattern validator.

Upvotes: 0

Abdalla Arbab
Abdalla Arbab

Reputation: 1400

you should just add

return true == (/^[a-z-_]+$/.test(value));

Demo

Upvotes: 1

Related Questions