Expand my Community achievements bar.

setFocus to field after formatMessage

Avatar

Former Community Member
Hello,



I'm using the following javascript code in the validate event of a date field:

//---------start script-----------

this.formatMessage="wrong format";



var test = datumObject.checkDate(this);



if(test == false){

this.validationMessage="date cannot be in the future";

false;

}

else{

true;

}

//----------end script----------



For this field I have set the Validation Pattern and the Edit Pattern to "DD-MM-YYYY".



Now when I enter a date with the format, for example only characters, I do get the proper message "wrong format". But I also want to have the focus set back to this field after this message.



Any ideas anyone? Is there maybe some function that I can use to check if the format of the entered value is correct?
2 Replies

Avatar

Former Community Member
You would use the command xfa.host.setFocus("FieldName") to set the cursor into your field.

Avatar

Former Community Member
Hi,



I found a solution that works for me. I do the following:



In the validate event:

----------------validate event start -----------------------

this.formatMessage="A date must have format dd-mm-yyyy hebben, e.g. 12-03-2008";



var test = dateObject.checkDate(this);



if(test == "nomatch"){

false;

}

else if(test == "futuredate"){

this.validationMessage="The entered date can not be in the future;

false;

}

else if(test == "currentdate"){

this.validationMessage="The entered date can not be equal to today's date";

false;

}

else {

true;

}

----------------validate event end-----------------------



In the exit event:

----------------exit event start -----------------------

if(this.rawValue != null && this.rawValue != ""){

var test = dateObject.checkDate(this);



if(test == "nomatch" | test == "futuredate" | test == "currentdate"){

xfa.host.setFocus("this");

}

}

----------------exit event end-----------------------



I included a scriptobject named 'dateObject':

----------------scriptobject start -----------------------

function checkDate(field){



if(field.rawValue != null && field.rawValue != ""){

var MyDate = util.scand("dd-mm-yyyy",new Date());



var entered = util.scand("dd-mm-yyyy", field.formattedValue);



var RegExPattern = /(0[1-9]|[12][0-9]|3[01])[- ](0[1-9]|1[012])[- ](19|20)\d\d/;

var errorMessage = 'Please enter a valid date like day, month and year(4 numbers).\nUse a '-'sign to separate the values.\nThe date must be a realistic one. For example 2-30-2000 wouldn't be accepted.\Format mm-dd-YYYY.';

if (!(field.formattedValue.match(RegExPattern)) && field.rawValue != null ) {

return "nomatch";

}

else if(!(field.formattedValue.match(RegExPattern)) && field.rawValue == null ){

return "nomatch";

}



else if ( MyDate.valueOf() < entered.valueOf() ) {

return "futuredate";

}

else if ( MyDate.valueOf() == entered.valueOf() ) {

return "currentdate";

}

else{

return "ok";

}



}

}

----------------scriptobject end -----------------------