parsley.js自定义validate
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 thatparsley.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>
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22