简体   繁体   中英

Regular Expression Checking For Proper Extension Names

I am an issue with my javascript calling valid extensions invalid when they should pass. Ive used this regular expression in server side code and it worked fine for me. I verified that the value the reg expression is checking against is valid as well.

Am I declaring reg expression wrong maybe in the javascript?

 var ck_name = /^.+\.((gdf)|(GDF))$/;
 var chldValue = chld.value.substring(chld.value.length - 4, chld.value.length);
    alert(chldValue);

    if (!ck_name.test(chldValue)) {
        errors[errors.length] = "File is NOT a GDF file";
}

Firstly, ^.+ is unnecessary and timewasting.

Second, your string is only four character long and what you are looking for... is a minimum of five characters long. Consequently they will never match.

Third, a regex is overkill.

Finally, your code should be:

if( chld.value.substr(chld.value.length-4).toLowerCase() != ".gdf")
    errors.push("File is NOT a GDF file");

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