admin管理员组文章数量:1405583
I am trying to validate a simple form using parsley.js and I am very beginner on parsley.js.I would like to show multiple error messages in one custom validation method using window.ParsleyValidator.addValidator() method.So I have tried out in my way.
Simple html form I have created
<html>
<head>Custom Validation Parsley
<script src=".10.1.min.js"></script>
<script src="js/parsley.min.js"></script>
<script src="js/my-validator-parsley.js"></script>
</head>
<body>
<form class="cmxform" id="mentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a ment</legend>
<p>
<label for="cemail">E-Mail with custom method (required)</label>
<input id="cemail" name="checkEmail" data-parsley-checkEmail>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#mentForm").parsley();
</script>
</body>
</html>
I am trying to validate a simple form using parsley.js and I am very beginner on parsley.js.I would like to show multiple error messages in one custom validation method using window.ParsleyValidator.addValidator() method.So I have tried out in my way.
Simple html form I have created
<html>
<head>Custom Validation Parsley
<script src="http://code.jquery./jquery-1.10.1.min.js"></script>
<script src="js/parsley.min.js"></script>
<script src="js/my-validator-parsley.js"></script>
</head>
<body>
<form class="cmxform" id="mentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a ment</legend>
<p>
<label for="cemail">E-Mail with custom method (required)</label>
<input id="cemail" name="checkEmail" data-parsley-checkEmail>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#mentForm").parsley();
</script>
</body>
</html>
And the javascript file that is included the custom validation method
var errorMsg = "";
window.ParsleyValidator.addValidator('checkEmail', function(value) {
console.log(value);
if (value == "") {
errorMsg = "this field must not be empty";
return false;
} else {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!regex.test(value)) {
errorMsg = "this field must be email ***";
}
return regex.test(value);
window.ParsleyValidator.addMessage('en', 'checkEmail',
errorMsg);
}
}, 32)
//.addMessage('en','checkEmail',window.ParsleyValidator.catalog)
;
But It's not working for me.Can anyone give me advice on this and how to approach this task?
Share Improve this question asked May 10, 2015 at 18:21 satkunam rajeenthinisatkunam rajeenthini 1514 silver badges12 bronze badges1 Answer
Reset to default 5You can only assign one message per constraint in a given locale.
From http://parsleyjs/doc/annotated-source/validator.html:
addMessage: function (locale, name, message) {
if ('undefined' === typeof this.catalog[locale])
this.catalog[locale] = {};
this.catalog[locale][name.toLowerCase()] = message;
return this;
},
Thus if you want multiple messages, define multiple validators.
Also,
if you want a field to be required there's no need to check if it's empty; just add
required
attribute to it (e.g.<input ... required />
)Parsley is bundled with mon validators (http://parsleyjs/doc/index.html#validators) and "email" is one of them; to enable it add attribute
type="email"
to your input (e.g.<input type="email" required />
)
UPDATE:
From http://parsleyjs/doc/index.html#psly-ui-for-javascript:
window.ParsleyUI.updateError(parsleyInstance, name, message);
Manually edit an error message.
Also, you can set the message from your validator function by directly assigning it to the catalog
instead of using addMessage()
(note that the name of the validator should be all lowercase):
window.ParsleyValidator.addValidator('checkEmail', function(value) {
if (...)
window.ParsleyValidator.catalog.en.checkemail = 'Some error message AAA';
else
window.ParsleyValidator.catalog.en.checkemail = 'Some error message BBB';
return (...);
}, 32);
本文标签: javascriptmultiple custom error message support per field by using parsleyjsStack Overflow
版权声明:本文标题:javascript - multiple custom error message support per field by using parsley.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744901977a2631397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论