var regex = /[A-Za-z]\\d[A-Za-z] ?\\d[A-Za-z]\\d/; var match = regex.exec(value); if (match){ if ( (value.indexOf(\"-\") !== -1 || value.indexOf(\" \") !== -1 )
regex = new RegExp(/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i); if(regex.test(value)) return true; else return false;
This is a shorter version of the original problem, where value is any text value. Furthermore, there is no need to test for value length.