Efficient regex for Canadian postal code function

前端 未结 7 1441
说谎
说谎 2020-12-24 01:57
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 )          


        
7条回答
  •  清歌不尽
    2020-12-24 02:12

    This is a function that will do everything for you in one shot. Accepts AAA BBB and AAABBB with or without space.

    function go_postal(){
            let postal = $("#postal").val();
            var regex = /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/;
            var pr = regex .test(postal);
            if(pr === true){
                //all good          
            } else {
                // not so much
            }
        }
    

提交回复
热议问题