简体   繁体   中英

Sanity check on form validation. Not accepting valid dates when my regex should do so

Here I have a Javascript function that when called, looks for three specific fields in an ASP form and checks each of them is filled in correctly. My intention is to then write some more code to do something with the data before a seperate asp submit button sends the form, but I don't think the asp stuff is relevant to the problem/question.

    function generateRef() {


var recieptno = document.getElementById("test_txt_Recno").value;
var dop = document.getElementById("test_txt_Date").value;
var branchno = document.getElementById("test_txt_Branchno").value;

if (typeof recieptno === undefined || recieptno =="" )  {
    alert('Missing Reciept Number');
                                                            }
else {
        if (typeof dop === undefined || dop =="" ) {
            alert('Missing Date of Purchase');
                                                            }
        else {
                if (typeof branchno  === undefined || branchno =="" )   {
                        alert('Missing Branch Number');
                                                                            }
                else {
                        // all is good, carry on.
                        // check for valid  reciept no
                        var pattern = new RegExp("^[0-2]([0-9]{5})$");

                        if ( pattern.test(recieptno)) {
                            // check for valid  branch no
                            var pattern2 = new RegExp("^([0-9]{4})$");

                            if ( pattern2.test(branchno)) {
                                // check for valid  dop
                                var pattern3 = new RegExp("^\d{2}[\/]\d{2}[\/]\d{4}$");

                                alert(dop);
                                if ( pattern3.test(dop)) {
                                    alert('OK');
                                    // more code will go here for the next steps
                                                                    }
                                else {
                                    alert('Invalid Date of Purchase. The date must be in DD/MM/YYYY format.');
                                        }
                                                                        }
                            else {
                                alert('Invalid Branch Number');
                                    }
                                                                    }
                        else {
                            alert('Invalid Reciept Number');
                                }

                        }
                }
        }

}

Note: The regex currently in the above code is just a poor mans date check, it's only there because I had a much better one, but wanted to make sure the regex was not the problem with a simple one.

I tried entering the date into the form of, for example 02/02/2020 and the alert spits out this date just fine that looks like it should work, but the logic of the code is still making it print out the alert that says the date is in the wrong format, not get to the 'OK' alert where I want to contine my code. The other two checks seems to be working that the date one is nestled inside, and they look the same format to me.

I just need a sanity check to spot my mistake at this point. I can't see why it's not working.

If using the RegExp constructor with a string literal, remember that the backslash is an escape in string literals, so to use it in the regular expression, you need to escape it at the string literal level. /a\*b/ and new RegExp("a\\*b") create the same expression, which searches for "a" followed by a literal "*" followed by "b".

In case you want to use RegExp constructor instead of /^\d{2}[\/]\d{2}[\/]\d{4}$/ , you need to escape backslashes: new RegExp("^\\d{2}[\\/]\\d{2}[\\/]\\d{4}$")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM