jQuery UI Widgets › Forums › Plugins › Validator, Drag & Drop, Sortable › Knockout and Validator problem
This topic contains 2 replies, has 1 voice, and was last updated by anubis76 9 years, 9 months ago.
-
Author
-
Hi Team,
I have a Default form based on a KnockOut ViewModel. When the Submit button is clicked, it calls a SaveData function in the ViewModel. The validate method is called and it works fine.
However, when the page is reloaded with the user’s id in the Query String, the Validate method, or any other method of the Validator plugin is just not called at all.
self.SaveData = function (vm) { $('#divBasicDetails').jqxValidator('validate'); $('#divBasicDetails').on('validationError', function (event) { $("#wrongNotification").jqxNotification("open"); }).on('validationSuccess', function (event) { ......save data Ajax function }); }
This works the first time only. When the page is loaded with a QueryString like this:
self.LoadData = function (appid) { $.ajax({ url: "Default.aspx/LoadBasicDetails", data: JSON.parse(JSON.stringify('{ \'appid\': \'' + appid + '\' }')), dataType: "json", type: "POST", async: false, contentType: "application/json; charset=utf-8", error: function (jqXHR, textStatus, errorThrown) { alert(textStatus + ", err: " + errorThrown); }, success: function (response) { var d = JSON.parse(response.d) if (d == "") { alert("AppID is invalid!"); window.location = "Default.aspx"; } else { masterModel.vm.vmdata.removeAll(); ko.mapping.fromJS(d[0], {}, masterModel.vm); alert('Successfully loaded data.'); } } });
This loads all my models data. The SaveData function is called when the button is clicked. But the Validator just does not work.
Any idea as to what I am doing wrong?
Thanks,
AJI have narrowed down the source of the problem. I am using the validator with a ComboBox and for some reason, only if I change a value in the ComboBox and then change it back, the Validator starts to work. Very strange because it does not seem to happen with any of the other components I am validating. Here is the code for the Validator:
When the combobox is Selected, I am storing the value in a variable, ccode.
$('#divBasicDetails').jqxValidator({ hintType: 'label', position: 'center', animationDuration: 0, rules: [ { input: '#txtAppName', message: 'Applicant Name is required!', action: 'blur', rule: 'required' }, { input: '#sexF', message: 'Please select a Gender', action: 'change', rule: function () { var checked1 = $("#sexM").is(':checked'); var checked2 = $("#sexF").is(':checked'); return (checked1 || checked2); } }, { input: '#txtEmail', message: 'E-mail is required!', action: 'blur', rule: 'required' }, { input: '#txtEmail', message: 'Please enter a valid email!', action: 'blur', rule: 'email' }, { input: '#txtResM', message: 'Please enter a mobile number!', action: 'blur', rule: 'required' }, { input: '#txtResM', message: 'Please enter a valid number!', action: 'blur', rule: 'number' }, { input: '#txtResM', message: "Mobile Number should be 10 digits long!", action: 'change', rule: function () { if (ccode == '91') { var num = $('#txtResM').prop('value'); if (num.replace(/[^0-9]/g, "").length != 12) return false; else return true; } } } ] });
Hi again,
Sorry, the problem was a mistake on my part. I was not setting the ccode variable when the form reloaded, so the combo-box validator returned neither a true nor a false, so it did not run at all. Problem solved.
-
AuthorPosts
You must be logged in to reply to this topic.