Hi, this is my firs post ans I’m novice in all this just trying to make my way out here. Also I would like to apologise in advans for my bad language.
So, on to my problem, as subject states I’m having trouble validate some input field using my own function. Code:
Colleague from work provided an older version of ‘dhtmlxform.js’ and then my example worked out!
Why ‘dhtmlxform.js’ which I took from ‘…\dhtmlxSuite_v35_pro_120822\dhtmlxForm\codebase’ (it’s where I extracted DHTMLX 3.5 files) isn’t working is beyond me?!
To make things even odder, when I applied new ‘dhtmlxform.js’ file to my actual project (in opening post code provided by me is just small example) my form gets all assorted.
Ok, I sorted it out with ‘dhtmlxform.js’ so please forget about it, my mistake. But problem persist, the only way for my alert window to show is to set ‘value’ attribute to not empty, so
Instead of:
Ok, so what is yours advice how to accomplish custom validation for this? I don’t want to populate my imput field with anything when loading my form (so I put
value:""
).
Upon button press event I want to execute my own validation (custom rule) which will not get fired if inital state of attribute value isn’t set to non empty string?? Please note that I don’t want to use dhtmlx built in validation rule “NotEmpty”, instead I want my own function to be called for validation purposes.
Thanks for your input on this.
What I was trying is: whem the button is pressed check and see if input field is empty, if so display message box (dhtmlx message box) displaying “Please enter data…!” and then return false so that input field can get “all red”.
if input empty and validation rule NotEmpty - fire validation, otherwise - no value = nothing to validate
if input not empty - any validation will fired
in your case your validation rule will not work while input empty. if you still need at least any validation - you can use them both, validate:“NotEmpty,YourCustomFunc”
In case someone will ever be wondering same thing, this is how I did it:
var myForm, formData;
function doOnLoad() {
formData = [
{type:"settings",position:"label-top"},
{type: "input", name: 'Name', label: 'Name:'},
{type:"input", name:"Age", label:"Age:"},
{type: "button", name:"Confirm", command:"confirm", value:"Confirm"}
];
myForm = new dhtmlXForm("form_container", formData);
myForm.attachEvent("onButtonClick", function(id){
MyValidate();
})
}
function MyValidate()
{
var data = myForm.getItemValue('Name');
if(data=="")
{
// display yours message box
myForm.setValidateCss("Name", "Error");//this will set desired "all red state"
return;
}
myForm.resetValidateCss("Name");//this will reset element state to normal
/*
//this will reset all form's element state to 'normal'
myForm.resetValidateCss();
*/
}
Maybe I didn’t provide sufficient code, my real form is much larger. Builtin funcionallity will mark all fields “red” which don’t pass validation rule. What I wanted is to go step-by-step, or to be more precise field-by-field:
Is fild ‘no. 1’ ok?
1a. If not display message box, mark him “red” and return
1b. If OK go to field ‘no.2’ do the same for it and all other fields
I know this is an old post, but this is something I have come up against.
Basically, I now ignore all the built-in validationrules and have created custom versions. You still have the problem of the function not being called when the input value is empty (duh!? - if empty is acceptable it should be part of the validation!), so to overcome this I commented out the following lines in the dhtmlx.js file (around line 4508 for the compiled version):
var r = true;
for (var q=0;q<item._validate.length;q++){
var v = “is”+item._validate[q];
// ISNL - to call validation function even when value is empty
// if ((val == null || val.length == 0)&& v != “isNotEmpty”) {
// }else {
var f = dhtmlxValidation[v];
if (typeof(f)!= “function” && typeof(item._validate[q]) == “function”) f = item._validate[q];
if (typeof(f)!= “function” && typeof(window[item._validate[q]]) == “function”) f = window[item._validate[q]];
r = ((typeof(f)==“function”?f(val):new RegExp(item._validate[q]).test(val)) && r);
f = null;
// }
}