EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Darrel on June 17, 2016, 07:22:03 AM



Title: Validate Combobox Options [Solved]
Post by: Darrel on June 17, 2016, 07:22:03 AM
Hello,

I'm having a select box which gets the data to be displayed as option tags using an ajax call. I then initial the select box with the easyui combobox. The code for the same is given below:

Code:
opt_str = "";  //value comes from the ajax call

$('#select').html(opt_str);

//tried using this but it is giving me the old value and new value.
$('#select').combobox({
onChange: function(newValue, oldValue){
alert("newValue: " + newValue + "\noldValue: " + oldValue);
}
});


I want to ensure that the value entered by the user in the editable combobox is present in the dropdown list after the user has tabbed out from the combobox or the combobox has lost the focus. I want to compare the text with the value of the combobox. If the value is not present in the list then an error message will be shown to the user. How can I achieve the same??

Thanks in advance...

Regards,
Darrel


Title: Re: Validate Combobox Options
Post by: jarry on June 17, 2016, 07:29:19 AM
Please refer to this topic http://www.jeasyui.com/forum/index.php?topic=3364.msg7623#msg7623


Title: Re: Validate Combobox Options
Post by: Darrel on June 17, 2016, 07:43:28 AM
Thanks jarry for your quick reply.

Is there anyway in which I can focus on the control again, if the value is not proper? Also please could you tell me how to retrieve the value of the option tag as well using combobox.

Thanks.


Title: Re: Validate Combobox Options
Post by: Darrel on June 20, 2016, 12:09:58 AM
Hello,

I was able to access the value of the drop down using the following line of code inside the for loop:

Code:
var dropdown_value = data[i]["value"];

To achieve my validation requirements, I added a global boolean variable g_bln_validValue and used it as a flagging value. If the flag is true, then the value is present in the drop down list. I then check the value if the flag in my function loadPageData. The modified code is as follows:

Code:
g_bln_validValue = false;		//added at the top in my html page code

$.extend($.fn.validatebox.defaults.rules,{
inList:{
validator:function(value,param){
var c = $(param[0]); //param[0] = field id
var opts = c.combobox('options');
var data = c.combobox('getData');
var exists = false;
for(var i=0; i<data.length; i++)
{
//if (value == data[i][opts.textField])
if (value.toLowerCase() == data[i][opts.textField].toLowerCase())
{
exists = g_bln_validValue = true;
g_str_dropDownValue = data[i][opts.textField];

var dropdown_value = data[i]["value"];

//console.log("value.toLowerCase(): " + value.toLowerCase() + "\tdata[" + i + "][opts.textField].toLowerCase(): " + data[i][opts.textField].toLowerCase() + "data[" + i + "][\"value\"]: " + data[i]["value"]);

loadPageData();
break;
}
}

g_bln_validValue = exists;

return exists;
},
message:'invalid value.'
}
});

Thanks & Regards,
Darrel.


Title: Re: Validate Combobox Options [Solved]
Post by: anugrast on October 02, 2016, 08:00:15 PM
How could we modify this to work with a pre-existing value in Edit Mode? Because I always get error when in Edit Mode...

Thx


Title: Re: Validate Combobox Options [Solved]
Post by: Darrel on October 06, 2016, 04:02:02 AM
Hello anugrast,

Please could share some code that you've done so far, so that I would be able to understand your scenario better. If possible could you add it in as a reloado code using the following url: http://code.reloado.com (http://code.reloado.com)

Regards,
Darrel