最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - multiple custom error message support per field by using parsley.js - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

You 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);
发布评论

评论列表(0)

  1. 暂无评论