javascript - Dojo/Dijit setting invalid message and failing validation -
i have form item missingmessage , invalidmessage defined item. call form validation method on item validation. if item empty missingmessage called , red exclamation mark error icon shown.
i trying set invalidmessage on text item when user enters specific value how can invalidmessage , red exclamation mark show without hard coding constraint on text item.
i use custom validation on items. if user enters particular value fail validation , return error. here trying tell user cannot enter value 'john' in first name field. getting red exclamation mark set invalidmessage not showing.
i when user clicks on exclamation mark can see error message , when begin type or change contents of field exclamation mark nad error message no longer shown.
jsp
<body class="claro"> <form id="myform" data-dojo-type="dijit/form/form"> <input data-dojo-type="dijit/form/validationtextbox" data-dojo-props=" regexp: '[\\w]+', required: true, invalidmessage: 'invalid first name !', missingmessage: 'first name required !'" id="fnametextbox" title="first name" placeholder="your first name" /> <input data-dojo-type="dijit/form/validationtextbox" data-dojo-props=" regexp: '[\\w]+', required: true, invalidmessage: 'invalid last name !', missingmessage: 'last name required !'" id="lnametextbox" title="last name" placeholder="your last name" /> <button id="validatefields" data-dojo-type="dijit/form/button">validate</button> </form> </body> javascript
dojo.require("dijit/form/form"); dojo.require("dijit/form/button"); dojo.require("dijit/form/validationtextbox"); dojo.require("dijit/tooltip"); dojo.ready(function() { var fname = dijit.byid("fnametextbox"); var lname = dijit.byid("lnametextbox"); dojo.connect(dijit.byid("validatefields"), "onclick", function() { var myform = dijit.byid('myform'); myform.connectchildren(); if(fname == "john"){ dijit.byid("fnametextbox")._set("state","error"); dijit.byid("fnametextbox").attr('_errormessage', 'john not allowed'); }else{ myform.validate(); } }); });
the best practice method provide separate validation rules particular widget overriding default validator() method of widget.
var fnametextbox = dijit.byid('fnametextbox'); fnametextbox.validator = function() { if(this.value.indexof("john")>-1) { this.set("invalidmessage",this.value+" not allowed!"); return false; } return true; } //below 2 lines programmatically validate specific field fnametextbox._hasbeenblurred = true; fnametextbox.validate(); //to show message on failing field must focussed fnametextbox.focus(); the validator function can have own logic validating , subjective field widget. if same has applied more one, define function instead of anonymous, , use respective fields.
but, if value check, can give regexp expression this.
regexp: '^((?!john).)*$' the following not best practice it. because calling form.validate() still validate field true, not correct.
dijit.byid("fnametextbox")._set("state","error"); dijit.byid("fnametextbox").attr('_errormessage', 'john not allowed');
Comments
Post a Comment