function closeDialogIfValid( dialogId ){
var invalidCount = dojo.query( '[id="' + dialogId + '"] [aria-invalid="true"]' ).length;
if( invalidCount === 0 ){
XSP.closeDialog( dialogId );
}
}
This probably only works if you're using server side validation.
If there is something like this in the XSP API, please let me know.
Share and enjoy!