Show asterisks or bullets instead of text on focusout - jquery - jquery-validate

I have a textbox where I need the text to become bullets after its validated. I am using jquery validation plugin to validate onfocusout. After the textbox is validated, if it is valid, I need the text to become asterisks or bullets (like a password textbox).
I can't just change the text to '*' because I need to keep the value of the textbox because otherwise, when the page validates again, it says it is invalid.
I don't want to have 2 different textboxes that are hidden/shown because it flashes in IE and looks very unprofessional and it also presents validation issues.
I can't change the 'type' of the textbox because that is not supported in IE.
Any other ideas? I searched a lot but have not come up with anything that works for me. Thanks!
Here is the code I have so far (following Eric's suggestion):
I have a textbox and a hidden field:
<asp:TextBox ID="txtProvId" runat="server"></asp:TextBox>
<asp:HiddenField ID="hdfProvId" runat="server"></asp:HiddenField>
The validation (on the hidden field):
$("form").validate(
{
onfocusout: function (element) {
jQuery(element).valid();
},
//error is placed in the textbox's tooltipster
errorPlacement: function (error, element) {
$(element).tooltipster('update', $(error).text());
$(element).tooltipster('show');
},
//tooltipster is removed when validation is passed
success: function (label, element) {
$(element).tooltipster('hide');
}
});
$('[id$="hdfProvId"]').each(function () {
$(this).rules('add', {
required: true,
number: true,
minlength: 7,
maxlength: 7
});
});
onFocusOut event on the textbox:
$('[id$="txtProvId"]').bind("focusout", function (event) {
$('[id$="hdfProvId"]').val($(this).val());
alert($('[id$="hdfProvId"]').val());
var isValid = $('[id$="hdfProvId"]').valid();
if (isValid == true) {
$(this).val('*******');
}
});
All this works great except that I don't know how to get the validation error to display. I am displaying the errors using jquery tooltipster plugin. It displays a tooltip next to each field with an error. When the validation is on the hidden field, no tooltip displays. I need it to display the tooltip on the textbox. Here is the tooltip code:
$('form input[type=text], input[type=password]').tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right'
});
The code to display it is above in the valiation code. Thanks for your help.

I added code to create a tooltip for hidden fields:
$('form input[type=text], input[type=password], input[type=hidden]').tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right'
});
The hidden field's tooltip was showing at the top of the page so I changed it to show on the previous element:
errorPlacement: function (error, element) {
if ($(element).prop('type') == 'hidden') {
$(element).prev().tooltipster('update', $(error).text());
$(element).prev().tooltipster('show');
}
else {
$(element).tooltipster('update', $(error).text());
$(element).tooltipster('show');
}
},
//tooltipster is removed when validation is passed
success: function (label, element) {
if ($(element).prop('type') == 'hidden') {
$(element).prev().tooltipster('hide');
}
else {
$(element).tooltipster('hide');
}
}
And now in all works!
Thanks all for you help in pointing me in the right direction.

Related

Custom Validation Rules do not work for Webix Colorpicker

I'm using webix as my JavaScript front end.
I've created a form with a colorpicker view. In Webix, by default, the colorpicker only allows users to pick from a pre-selected range of colors, and returns the hex code of the color selected.
This behavior can be overridden to allow entering of any color by using the option editable: true. However, editable: true allows users to enter anything into the colorpicker as if were a free text field.
I'm trying to use a custom validation to work around this. I should be able to return false in the custom validation function to alert the user user of an invalid value and to prevent the form from being saved until it's fixed. However, the custom validation function never gets called when used on the colorpicker.
Here's a webix snippet of what I'm trying:
https://snippet.webix.com/28oadxzl
webix.ui({
view:"form",
id: "theForm",
name: "theForm",
elements:[
{
view:"colorpicker",
label:"color",
name:"color",
id: "color",
editable: true,
validate: webix.rules.iscolor,
on: {
onChange: function(newv, oldv) {
// this gets called every time the color picker is changed.
webix.message({text: "onChange: " + newv})
this.validate();
}
}
},
{
view:"button",
type:"form",
value:"ok",
width:200,
align:"right",
click: function() {
// this gets called when the "ok" button is clicked.
webix.message({text: "click"});
$$("theForm").validate();
}
}
],
rules: {
iscolor: function(value) {
// never gets called! Should be called on colorpicker change and on "ok" click.
return false; // replace with regex hexcode validation.
webix.message({text: "iscolor: " + value})
}
}
});
You were almost right: https://snippet.webix.com/4mw3mxk8
The thing is that:
as a key in rules, you must use the name of the control ("color" in your case)
webix.rules includes only predefined validation rules (isEmail, isNotEmpty, etc)

Dynamically change a column's editable property with select box

I am using form editing. I would like to disable certain fields in my add and edit forms based on the selection from a drop down box. What event is best to use to trigger this? I have attempted using dataEvents:
{ name:'type_cd',
edittype:'select',
editoptions:{
dataUrl:'functions.php',
dataEvents:[{
type:'change',
fn: function(e){
$(this).setColProp('cntrct_id',{
editoptions:{editable:false}
});
}
}]
}
},
This has no visible effect on my form fields, but I know that it's being reached because I can get an alert message from it if I put one in.
EDIT
If I submit the form, the next time I open it, the column that was set to editable:false will not appear. This is a step in the right direction, BUT I want it to immediately be uneditable. Really, I would like it to be visible, but disabled (i.e. disabled:true)
First of all dataEvents allows you to register callbacks on elements of edit elements. Inside of callbacks this will be initialized to the DOM element which will be bound. So $(this) inside of change handler it will be wrapper on <select> element and not on the grid. The usage of $(this).setColProp will be incorrect.
To disable some input field in Add/Edit form you can use the fact that all input elements get the same id like the value of name property on the corresponding column in colModel. So if you need to disable input of cntrct_id you can set disabled property to true on the element with id="cntrct_id"
{
name: 'type_cd',
edittype: 'select',
editoptions: {
dataUrl: 'functions.php',
dataEvents: [{
type: 'change',
fn: function (e) {
// disable input/select field for column 'cntrct_id'
// in the edit form
$("#cntrct_id").prop("disabled", true);
}
}]
}
}
It's important to understand that editoptions will be used for any existing editing modes (form editing, inline editing and cell editing). If you want to write the code of dataEvents which supports all editing modes you have to detect editing mode and use a little other ids of editing fields. The code (not tested) can be about as below
{
name: 'type_cd',
edittype: 'select',
editoptions: {
dataUrl: 'functions.php',
dataEvents: [{
type: 'change',
fn: function (e) {
var $this = $(e.target), $td, rowid;
// disable input/select field for column 'cntrct_id'
if ($this.hasClass("FormElement")) {
// form editing
$("#cntrct_id").prop("disabled", true);
} else {
$td = $this.closest("td");
if ($td.hasClass("edit-cell") {
// cell editing
// we don't need to disable $("#cntrct_id")
// because ONLY ONE CELL are edited in cell editing mode
} else {
// inline editing
rowid = $td.closest("tr.jqgrow").attr("id");
if (rowid) {
$("#" + $.jgrid.jqID(rowid) + "_cntrct_id")
.prop("disabled", true);
}
}
}
}
}]
}
}
The last remark. If you still use old version of jQuery (before jQuery 1.6) which don't support prop method you have to use attr instead.
#Oleg: This is working(could get the alert messages) but its not disabling the field.
Should the form field require any special values?

jqGrid inline edit: odd behavior with an autocomplete column

I have a jqGrid (using inline editing) with an autocomplete column. When the user selects a value from the autocomplete column, an event handler sets a value on another column, and also sets the value on the autocomplete column to something other than the label returned from the autocomplete source. The two column definitions (complete jsFiddle example here):
{
name: 'cartoonId',
index: 'cartoonId',
width: 90,
editable: false},
{
name: 'cartoon',
index: 'cartoon',
width: 200,
editable: true,
edittype: 'text',
editoptions: {
dataInit: function(elem) {
$(elem).autocomplete({
source: autocompleteSource,
select: function(event, ui){
var rowId = $("#inlineGrid").jqGrid('getGridParam', 'selrow');
if(ui.item){
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoonId', ui.item.CartoonId);
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoon', ui.item.Name);
}
return false;
}
});
}
}},
The problem is that whenever the user selects a value from the autocomplete, either by clicking it or using arrows and pressing the tab key, that cell is no longer editable, and the grid appears to lose focus entirely. If I comment out the line that sets the cartoon cell value, it behaves normally. Is there any way I can get around this behavior? I need the entire row to remain in edit mode, including the cartoon column, until the user completes the edit.
jqGrid 4.4.1
jQuery 1.7.2
jQuery UI 1.8.18
You should rename Name property of the items from the autocompleteSource to value because jQuery UI Autocomplete examines the label and value per default (see the documentation).
You can't use setCell of the 'cartoon' column which is currently in the editing mode. You should remove return false; from select callback too. So the code could looks about the following
dataInit: function (elem) {
$(elem).autocomplete({
source: autocompleteSource,
select: function (event, ui) {
var rowId = $("#inlineGrid").jqGrid('getGridParam', 'selrow');
if (ui.item) {
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoonId',
ui.item.CartoonId);
}
}
});
}
See http://jsfiddle.net/27dLM/38/

Adding Event Listeners to a class of XTemplate elements in a grid

I've a column of XTemplate cells in a Grid Panel. How do I add a click event/listener that applies to all cells on this particular column? What I've tried so far works but applies to ALL clicks on ANY cell in the grid. I can't seem to manipulate the delegate option to filter for a particular class of element.
My code so far:
columns:[
...
{
xtype: 'templatecolumn',
text: 'Approve2',
flex: 1,
dataIndex: 'Approved',
align: 'center',
sortable: false,
tpl: '<input type="checkbox" class="approveCheckbox" />'
},
...
],
initComponent: function () {
this.on('itemclick', this.storeCheckboxVal, this, { delegate: '.approveCheckbox' });
},
...
,
storeCheckboxVal: function (view, record, item, index, event) {
alert(record.data['ID']);
}
AFAIK, delegate works only if you assign handler to DOM Element (not Component). Try this code instead:
initComponent: function () {
this.mon(this.el, 'click', this.storeCheckboxVal, this, { delegate: '.approveCheckbox' });
},
You'll probably need to change your grid's selType to cellmodel. After that, you should be able to listen to the grid view's cellclick. This appears to be undocumented, but I found it using Ext.util.Obersvable.capture(Ext.getCmp('my-grid-id'), console.log) Which is an extremely useful trick to know.
Thanks for the replies all, however I've managed to solve my problem via this solution: Ext JS on click event

How to solve problem when use jquery datepicker and validation in the same time

When I used datepicker with trigger icon so that users could choose date from clicking this icon or type directly in textbox (txtDate), I also used jquery validation to require textbox must be not empty.
But when a user submit the form with empty textbox (txtDate.Text=""), the error message of validation push trigger icon to the right. Could you tell me the solution? Thank you very much!
$('#some_form').validate({
rules: {...},
messages: {...},
errorPlacement: function(error, element) { //solve you problem
var trigger = element.next('.ui-datepicker-trigger');
error.insertAfter(trigger.length > 0 ? trigger : element);
}
});
The errorPlacement function receives two parameters - the error message and the validated element. Use the latter to decide whether or not to customize the placement for your fields (for example, by adding a class):
$('form').validate({
rules: {...},
errorPlacement: function(error, element) {
if (element.hasClass('customError')) {
// custom error placement
}
else {
element.after(error); // default error placement
}
}
});

Resources