parsley.js自定义validate

linkincsdn 2014-10-30

1.window.ParsleyValidator

<!DOCTYPE html>
<html>
<head lang="en">
    <script type="text/javascript" src="jquery/jquery-1.11.1.min.js"></script>
    <link type="text/css" rel="stylesheet"  href="boot/bootstrap.css" />
    <script type="text/javascript" src="boot/bootstrap.js"></script>
    <script type="text/javascript" src="parsley/parsley-2.0.3.js"></script>
    <link type="text/css" rel="stylesheet" href="parsley/parsley.css"/>
    <meta charset="UTF-8">
    <title>ParsleyDemo</title>
    <script type="text/javascript" >
        window.ParsleyValidator
                .addValidator('notequal', function (value, elem) {
                    return value !== $( elem ).val();
                }, 32);

    </script>
</head>
<body>
<form id="myform" data-parsley-validate>
    <input id="input1" />
    <input type="text" class="form-control" name="why" data-parsley-notequal='#input1'
           data-parsley-notequal-message="can't equal!" />
    <input type="submit" />
</form>
</body>
</html>

2.window.ParsleyConfig

<!DOCTYPE html>
<html>
<head lang="en">
    <script type="text/javascript" src="jquery/jquery-1.11.1.min.js"></script>
    <link type="text/css" rel="stylesheet"  href="boot/bootstrap.css" />
    <script type="text/javascript" src="boot/bootstrap.js"></script>
    <link type="text/css" rel="stylesheet" href="parsley/parsley.css"/>
    <meta charset="UTF-8">
    <title>ParsleyDemo2</title>
    <script type="text/javascript" >
        window.ParsleyConfig = {
            validators: {
                notequal: {
                    fn: function (value, elem) {
                        return value !== $( elem ).val();
                    },
                    priority: 32
                }
            }
        };
    </script>
</head>
<body>
<form id="myform" data-parsley-validate>
    <input id="input1" />
    <input type="text" class="form-control" name="why" data-parsley-notequal='#input1'
           data-parsley-notequal-message="can't equal!" />
    <input type="submit" />
</form>
<script type="text/javascript" src="parsley/parsley-2.0.3.js"></script>
</body>
</html>

Craft yours!

Of course, Parsley built-in validators are commonly used validators, and you'll need some more that fit your specific forms and validations. That's why Parsley lets you easily create your own validators.

Here again, like localizations, configuring your custom validators and error messages comes with two flavors:

  • By registering them in some globals before calling parsley.js:
    <input type="text" data-parsley-multiple="3" />
    [...]
    <script type="text/javascript">
    window.ParsleyConfig = {
      validators: {
        multiple: {
          fn: function (value, requirement) {
            return 0 === value % requirement;
          },
          priority: 32
        }
      },
      i18n: {
        en: {
          multiple: 'This value should be a multiple of %s'
        },
        fr: {
          multiple: 'Cette valeur doit être un multiple de %s'
        }
      }
    };
    </script>
  • By registering them in ParsleyValidator after that parsley.js is loaded:
    <input type="text" data-parsley-multiple="3" />
    [...]
    <script type="text/javascript">
    window.ParsleyValidator
      .addValidator('multiple', function (value, requirement) {
        return 0 === value % requirement;
      }, 32)
      .addMessage('en', 'multiple', 'This value should be a multiple of %s')
      .addMessage('fr', 'multiple', 'Cette valeur doit être un multiple de %s');
    </script>

相关推荐