Monday, January 12, 2015

Check Special Character in File folder name using regular expression


In Javascript:

function checkFolderName(str) {
    strreg = /^[\w\-]+$/
    var strregx = new RegExp(strreg);
    if (strregx.test(str)) {
        //alert("OK")
        return false
    }
    else {
        //alert("Not OK")
        return true
    }
}

function checkFileName(str) {
    var name = str.substr(0, str.lastIndexOf('.'));
    var ext = str.substr(str.lastIndexOf('.') + 1);
    ext = ext.toLowerCase();
    //alert(name);
    //alert(ext);
    strreg = /^(?!.*  )(?!.*__)(?!.*--)[\w -]+$/    //Check alphanumeric with underscore and Hyphan and two consecutive spaces,underscore,hyphen
    //strreg = /^[a-zA-Z0-9_\-]+$/          //Check alphanumeric with underscore and Hyphen
    var strregx = new RegExp(strreg);
    if ((strregx.test(name)) && (ext != "asp") && (ext != "aspx") && (ext != "dll") && (ext != "cmd") && (ext != "bat") && (ext != "exe")) {
        //alert("OK")
        return false
    }
    else {
        //alert("Not OK")
        return true
    }
}
//-->

How To use function:

if(checkFolderName(document.getElementById("abc").value))
{
    alert("Please don't use special char in Foldername.")
}

if(checkFileName(document.getElementById("abc").value))
{
    alert("Please don't use special char in File Name.")
}

</SCRIPT>

In VBScript
<%

function checkFoldername(Foldername)
    dim RegularExpressionObject: Set RegularExpressionObject = New RegExp
    dim matches: matches=""
    RegularExpressionObject.Pattern = "^[\w\-]+$"
    matches = RegularExpressionObject.Test(Foldername)
    checkFoldername = matches
    set RegularExpressionObject = nothing
end function
function checkFilename(Filename)
    arrFname = split(Filename,".")
    strExtNum = ubound(arrFname)
    strExt = lcase(arrFname(strExtNum))
 
    for i = 0 to ubound(arrFname)-1
        if strfilename = "" then
            strfilename = arrFname(i)
        else
            strfilename = strfilename&"."&arrFname(i)
        end if
    next
    strOK = false
    dim RegularExpressionObject: Set RegularExpressionObject = New RegExp
    dim matches: matches=""
    'RegularExpressionObject.Pattern = "^[\w\-]+$"
    RegularExpressionObject.Pattern = "^(?!.*  )(?!.*__)(?!.*--)[\w -]+$"
    matches = RegularExpressionObject.Test(strfilename)
    if matches and strExt<>"asp"  and strExt<>"aspx" and strExt<>"dll" and strExt<>"cmd" and strExt<>"bat" and strExt<>"exe" then
        strOK = true
    else
        strOK = false
    end if
    checkFilename = strOK
    set RegularExpressionObject = nothing
end function

function checkUploadFilename(Filename)
    arrFnameS = split(Filename,"\")
    strfnameNum = ubound(arrFnameS)
    arrFname = split(arrFnameS(strfnameNum),".")
    strExtNum = ubound(arrFname)
    strExt = lcase(arrFname(strExtNum))
 
    for i = 0 to ubound(arrFname)-1
        if strfilename = "" then
            strfilename = arrFname(i)
        else
            strfilename = strfilename&"."&arrFname(i)
        end if
    next
    strOK = false
    dim RegularExpressionObject: Set RegularExpressionObject = New RegExp
    dim matches: matches=""
    'RegularExpressionObject.Pattern = "^[\w\-]+$"
    RegularExpressionObject.Pattern = "^(?!.*  )(?!.*__)(?!.*--)[\w -]+$"
    matches = RegularExpressionObject.Test(strfilename)
    if matches and strExt<>"asp"  and strExt<>"aspx" and strExt<>"dll" and strExt<>"cmd" and strExt<>"bat" and strExt<>"exe" then
        strOK = false
    else
        strOK = true
    end if
    checkUploadFilename = strOK
    set RegularExpressionObject = nothing
end function

%>


No comments:

Post a Comment